PHP数组随机HTML链接

Ok I'm going to try and explain this the best I can, I have 25 links in this format:

<a href="http://blabla.com" title="bla bla">bla bla</a>

First thing first, I need to add these 25 links into an array, which I am bit unsure of how to do it because its html, secondly I need to shuffle the array to choose 7 of them randomly and then display those 7.

Hope someone can help, this is beyond me, thanks in advance.


Ok, a little update, I have found a way of getting 1 html link to display randomly, could anyone help me with getting 7 out?

<?php
// Create the array
$links = array();

$links[0] = '<a href="http://bla1.co.uk" title="bla1">bla1</a>';
$links[1] = '<a href="http://bla2.co.uk" title="bla2">bla2</a>';
$links[2] = '<a href="http://bla3.co.uk" title="bla3">bla3</a>';
// Count links
$num = count($links);
// Randomize order
$random = rand(0, $num-1);
// Print random link
echo $links[$random];
?>

For your second task :

Check array_rand() to retrieve X random values in your array.

http://www.php.net/manual/en/function.array-rand.php

If you care only about displaying these links randomized to the user then you can do with JavaScript like this http://jsfiddle.net/hVZL2/.

If you want to load these links into PHP array and do something with them after you still will have to use JavaScript. Convert the array that I created to JSON, send it via POST to some script that will parse JSON and you will have array of links.


As I can see you have your links on server.
<?php
// Create the array
$links = array();

$links[0] = '<a href="http://bla1.co.uk" title="bla1">bla1</a>';
$links[1] = '<a href="http://bla2.co.uk" title="bla2">bla2</a>';
$links[2] = '<a href="http://bla3.co.uk" title="bla3">bla3</a>';
$links[3] = '<a href="http://bla3.co.uk" title="bla3">bla3</a>';
$links[4] = '<a href="http://bla3.co.uk" title="bla3">bla3</a>';
$links[5] = '<a href="http://bla3.co.uk" title="bla3">bla3</a>';
$links[6] = '<a href="http://bla3.co.uk" title="bla3">bla3</a>';

// Shuffle the array
shuffle($links);

// Display your links, note that we will display five links out of seven
for ($i = 0; $i < 5; $i++){
   echo $links[$i];
}