This article mainly introduces PHP function shuffle () take the array of several random elements of the method, combined with the case of the function of the shuffle function, definition, use of methods and related considerations, the need for friends can refer to the next
This paper describes the PHP function shuffle () to take the array of random elements of a method. Share to everyone for your reference, as follows:
Sometimes we need to take a number of random elements in the array (such as random recommendations), so how can PHP be implemented? A relatively simple workaround is to use PHP's own shuffle () function.
Shuffle ()
The shuffle () function rearranges the elements in the array in random order. Returns TRUE if successful, otherwise FALSE is returned. This function assigns the new key name to the cells in the array, which removes the existing key names and not just the reordering.
Here's a simple example:
$data [] = Array ( "name" = "Home of the Script", "rank" = "+" ), $data [] = Array ( "name" = "Blog Park", "rank" => ; $data [] = Array ( "name" = "CSDN", "rank" = "()"), $data [] = Array ( "name" = "Iteye", " Rank "+"), Shuffle ($data), $i = 0;foreach ($data as $key + $value) { if ($i < 2) { echo $data [$key] [Nam E ']. ' <br/> '; } $i + +;}