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

php shuffle() randomly sorts array code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:44:141016browse

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...