Home > Backend Development > PHP Tutorial > php shuffle() randomly sorts array code_PHP tutorial

php shuffle() randomly sorts array code_PHP tutorial

WBOY
Release: 2016-07-13 17:44:14
Original
1065 people have browsed it

The shuffle() function rearranges the elements in the array in random order.

Returns TRUE if successful, otherwise returns FALSE.

Note: This function assigns a new key name to the unit in the array. This will delete the original keys rather than just reorder them.

Note: As of PHP 4.2.0, it is no longer necessary to seed the random number generator with the srand() or mt_srand() functions, it is now done automatically

$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
shuffle($my_array);
print_r($my_array);
?>
Look at the following // Use an array to store the advertisement list

$ads = array(Ad 1< ;/a>
,
Ad 2< ;/a>
,
Ad 3< ;/a>
,
Ad 4< ;/a>
);

// Randomly sort the array
shuffle($ads);

// Output the sorted array
$html = ;
foreach ($ads as $ad) {
$html .= $ad;
}
echo $html;



www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478762.htmlTechArticleshuffle() function rearranges the elements in the array in random order. Returns TRUE if successful, FALSE otherwise. Note: This function assigns a new key name to the unit in the array. This...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template