PHP array element Random sort code

Source: Internet
Author: User
Tags php tutorial shuffle

PHP Tutorial array element Random sort code
Because the job requires me to randomly sort the array I've defined, and each time the loop is different, we use the PHP shuffle function to sort by random array elements. method is very simple.

The shuffle () function rearranges the elements in the array in random order.

If successful, returns TRUE, otherwise returns false.

Note: This function assigns a new key name to the cell in the array. This deletes the original key name and not just the reordering.

*/
$array = Array (1,2,3,4);
For ($i =1 $i <30; $i + +)
{
Shuffle ($array);
Print_r ($array);
echo ' <br/> ';
}
/*
Array ([0] => 3 [1] => 2 [2] => 4 [3] => 1)
Array ([0] => 3 [1] => 4 [2] => 1 [3] => 2)
Array ([0] => 2 [1] => 1 [2] => 3 [3] => 4)
Array ([0] => 2 [1] => 4 [2] => 1 [3] => 3)
Array ([0] => 3 [1] => 4 [2] => 2 [3] => 1)
Array ([0] => 3 [1] => 2 [2] => 4 [3] => 1)
Array ([0] => 3 [1] => 4 [2] => 1 [3] => 2)
Array ([0] => 4 [1] => 2 [2] => 1 [3] => 3)
Array ([0] => 2 [1] => 4 [2] => 1 [3] => 3)
Array ([0] => 4 [1] => 3 [2] => 1 [3] => 2)
Array ([0] => 3 [1] => 2 [2] => 1 [3] => 4)
Array ([0] => 4 [1] => 3 [2] => 1 [3] => 2)
Array ([0] => 4 [1] => 1 [2] => 2 [3] => 3)
Array ([0] => 2 [1] => 1 [2] => 4 [3] => 3)
Array ([0] => 3 [1] => 1 [2] => 4 [3] => 2)
Array ([0] => 2 [1] => 1 [2] => 3 [3] => 4)
Array ([0] => 4 [1] => 1 [2] => 3 [3] => 2)
Array ([0] => 1 [1] => 4 [2] => 2 [3] => 3)
Array ([0] => 3 [1] => 2 [2] => 1 [3] => 4)
Array ([0] => 1 [1] => 2 [2] => 4 [3] => 3)
Array ([0] => 1 [1] => 3 [2] => 4 [3] => 2)
Array ([0] => 2 [1] => 4 [2] => 3 [3] => 1)
Array ([0] => 3 [1] => 2 [2] => 1 [3] => 4)
Array ([0] => 1 [1] => 3 [2] => 4 [3] => 2)
Array ([0] => 2 [1] => 1 [2] => 4 [3] => 3)
Array ([0] => 4 [1] => 1 [2] => 3 [3] => 2)
Array ([0] => 3 [1] => 2 [2] => 1 [3] => 4)
Array ([0] => 1 [1] => 2 [2] => 3 [3] => 4)
Array ([0] => 2 [1] => 1 [2] => 4 [3] => 3)

Summarize:
PHP has a lot of functions, and if you know that this function will help you develop a lot, it may take a lot of time if you don't know what to write yourself.
*/

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.