PHP uses the array to disrupt the shuffle function and a simple instance _php instance

Source: Internet
Author: User
Tags shuffle

Shuffle ()

The PHP shuffle () function randomly arranges the order of array cells (the array is scrambled). This function assigns a new key name to the cell in the array, which deletes the original key name and not just the reordering.

Grammar:

BOOL Shuffle (array &array)

Example 1:

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

Run the example output:

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

It should be explained that each time the page is refreshed, the results of the Print_r ($arr) Shuffle ($arr) are different. And since PHP 4.2.0, also no longer need to use Srand () and other functions to the random number generator seeding and the system automatically completed.

Example 2, using an associative array:

<?php
$arr = Array ("A" =>1, "B" =>2, "C" =>3, "D" =>4, "E" =>5);
Shuffle ($arr);
Print_r ($arr);
? >

Run the example output:

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

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

The above is a small series for everyone to bring PHP to disrupt the use of shuffle functions and simple examples of all the content, I hope that we support cloud-Habitat Community ~

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.