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. Here's a simple example:
$data [] = Array ( "name" = "Site", "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 ']. '
'; } $i + +;}
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.
If you pass in an associative array, you will find that the key name of the associated array is missing. By the way, here's the solution:
The Sort function Shuffle_assoc ($array) { $randomized _keys = Array_rand ($array, Count ($array)) that disrupts the associative array; foreach ($randomized _keys as $current _key) { $output [$current _key] = $array [$current _key]; } return $output;}
In addition, PHP provides a function to randomly extract values from an array: Array_rand (), whose invocation format is as follows: Array_rand (array, number of extracted elements); Of course, to implement support for two-dimensional arrays needs to be encapsulated before they can be used. But the previous program is good for supporting two-dimensional arrays.
More readers interested in PHP related content can view this site: "Summary of PHP operations and operator usage," PHP Network Programming Tips Summary, "PHP Basic Grammar Primer Tutorial", "PHP operation Office Document tips summary (including word,excel,access, ppt), "PHP Date and Time usage summary", "PHP primer for Object-oriented programming", "PHP String Usage Summary", "Getting Started with Php+mysql database operations" and "PHP Common Database Operations Skills Summary"
I hope this article is helpful to you in PHP programming.
The above describes the PHP function shuffle array of several random elements of the method analysis, including the shuffle,php aspects of the content, I hope that the PHP tutorial interested in a friend helpful.