PHP will disrupt the shuffle function usage and simple instances, disrupt shuffle

Source: Internet
Author: User
Tags shuffle

PHP will disrupt the shuffle function usage and simple instances, disrupt shuffle

Shuffle ()

The PHP shuffle () function randomly sorts the order of array units (disrupt the array ). This function assigns a new key name to the Unit in the array. This will delete the original key name, not just the reorder.

Syntax:

Bool shuffle (array & array)

Example 1:

<?php$arr = range(1,8);print_r($arr);echo '<br />';shuffle($arr);print_r($arr);?>

Run the sample output:

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 ) Array ( [0] => 6 [1] => 1 [2] => 3 [3] => 2 [4] => 5 [5] => 7 [6] => 8 [7] => 4 ) 

Note that the result of print_r ($ arr) after shuffle ($ arr) is different each time you refresh the page. Since PHP 4.2.0, the system does not need to use functions such as srand () to seeding the random number generator.

Example 2: Associate an array:

<?php$arr = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);shuffle($arr);print_r($arr);?>

Run the sample output:

Array ([0] => 5 [1] => 2 [2] => 1 [3] => 3 [4] => 4)

Of course, the output results of each page refresh are different.

The above is the usage of shuffle function and simple instance of PHP, which will disrupt the array. I hope you can support more help ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.