Home  >  Article  >  Backend Development  >  What does php shuffle mean?

What does php shuffle mean?

青灯夜游
青灯夜游Original
2021-07-07 12:52:193027browse

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.

What does php shuffle mean?

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

What does php shuffle mean?

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

<?php
header("Content-type:text/html;charset=utf-8");
$info = array(&#39;颜色1&#39; => &#39;红色&#39;, &#39;颜色2&#39; => &#39;黄色&#39;, &#39;颜色3&#39; => &#39;蓝色&#39;, &#39;颜色4&#39; => &#39;紫色&#39;);
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
echo &#39;数组打乱顺序前:&#39;;
print_r($info);
echo &#39;数组打乱顺序后:&#39;;
shuffle($info);
print_r($info);
?>

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:

<?php
    function verCode(){
        $arr    = array_merge(range(&#39;A&#39;,&#39;Z&#39;), range(&#39;a&#39;, &#39;z&#39;), range(&#39;1&#39;, &#39;9&#39;));
        shuffle($arr);
        $newArr = array_slice($arr, 0, 4);
        $str    = implode(&#39;&#39;, $newArr);
        return $str;
    }
    echo verCode().&#39;<br>&#39;;
    echo verCode().&#39;<br>&#39;;
    echo verCode().&#39;<br>&#39;;
?>

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!

Statement:
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
Previous article:php pdo what does it meanNext article:php pdo what does it mean