In PHP, the Chinese meaning of shuffle is "shuffle". This function can randomly disrupt the array and rearrange the elements in the array in random order. The syntax format is "shuffle(array)". The shuffle() function assigns new keys to elements in the array, and existing keys are deleted.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
shuffle () function rearranges the elements in the array in random order.
The shuffle() function will not only randomly shuffle the array, but also delete the original key names of the array and re-create new key names. The syntax format of the function is as follows:
shuffle(array)
where array is the array to be operated on. The function returns TRUE if it succeeds and FALSE if it fails.
Tip: The shuffle() function is only applicable to the first dimension of the array, and is invalid for dimensions other than the first dimension in multi-dimensional arrays.
Example:Use the shuffle() function to disrupt the order of the array
'红色', '颜色2' => '黄色', '颜色3' => '蓝色', '颜色4' => '紫色'); echo ''; echo '数组打乱顺序前:'; print_r($info); echo '数组打乱顺序后:'; shuffle($info); print_r($info); ?>Copy after login
Output:
数组打乱顺序前:Array ( [颜色1] => 红色 [颜色2] => 黄色 [颜色3] => 蓝色 [颜色4] => 紫色 ) 数组打乱顺序后:Array ( [0] => 蓝色 [1] => 红色 [2] => 黄色 [3] => 紫色 )
With the array_rand() function Similarly, the shuffle() function can also be used to generate random verification codes. The sample code is as follows:
'; echo verCode().'
'; echo verCode().'
'; ?>
The running results are as follows:
WLmj ngqO EolQ
Note: Because the shuffle() function randomly changes the array Disruption, so the results of each execution will be different.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What does php shuffle mean?. For more information, please follow other related articles on the PHP Chinese website!