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
<?php header("Content-type:text/html;charset=utf-8"); $info = array('颜色1' => '红色', '颜色2' => '黄色', '颜色3' => '蓝色', '颜色4' => '紫色'); echo '<pre class="brush:php;toolbar:false">'; echo '数组打乱顺序前:'; print_r($info); echo '数组打乱顺序后:'; 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('A','Z'), range('a', 'z'), range('1', '9')); shuffle($arr); $newArr = array_slice($arr, 0, 4); $str = implode('', $newArr); return $str; } echo verCode().'<br>'; echo verCode().'<br>'; echo verCode().'<br>'; ?>
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!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.