Examples of random and reverse PHP arrays

Source: Internet
Author: User
The first two articles, "how to sort PHP arrays" and "reverse order of PHP arrays", introduce two groups of functions, one in ascending order and the other in descending order ), today, we will introduce the random and reverse orders of arrays in this article! PHP array random out-of-order and reverse-order

The first two articles, "how to sort PHP arrays" and "reverse order of PHP arrays", introduce two groups of functions, one in ascending order and the other in descending order ), today, we will introduce the random and reverse orders of arrays in this article!

1. what is random disorder? (Shuffle)

Shuffle is to disrupt the array.

The shuffle function can be used to randomly sort the elements in the array.

The shuffle syntax structure is as follows:

bool shuffle ( array &$array )
Parameters Description
Array Required. Specifies the array to be adapted.

This function breaks down an array (the order of random units. It uses a pseudo-random number generator and is not suitable for cryptography.

Shuffle instance usage:

The following is an example of shuffle function. the code is as follows:

 "PHP 文"", "url" => "www.php1.cn", "tom" => "100", "hank" => "60"); shuffle ($ arr ); // randomly sort the values of the array print_r ($ arr);?>

Output result:

The output results of the above instance are different every time you refresh the browser. there is not much here. you can try it locally!

Note: After the associated array is shuffled, the key will be lost, which can be seen from the above instance!

2. what is Reverse order? (Array_reverse)

Array_reverse-returns an array with the opposite unit order. The Reverse order is to flip the order of each element in an original array,

The syntax format of array_reverse is as follows:

array array_reverse ( array $array [, bool $preserve_keys = false ] )

Array_reverse () accepts array as input and returns a new array in reverse order. if preserve_keys is TRUE, the original key name is retained.

Note:

1. reverse sequence = descending order

2. if the array is an associated array

If the key is a character, the reverse order is not affected, and the key is retained.

When the key is a number, the default key is reset to 0, 1, 2 ......

When the second parameter is true, the key is a number, and the number key is retained in reverse order.

The following shows the sample code when the key is a number:

 "PHP 文"", "200" => "www.php1.cn", "300" => "Baidu", "400" => "Sogou "); $ array = array_reverse ($ arr); // reverse order of the array to reverse the elements of the array print_r ($ arr); echo"
"; Print_r ($ array);?>

Output result:

Sample code for retaining a key value when the key is a number:

 "PHP 文"", "200" => "www.php1.cn", "300" => "Baidu", "400" => "Sogou "); $ array = array_reverse ($ arr, true); // reverse order of the array, reversing the elements of the array print_r ($ arr); echo"
"; Print_r ($ array);?>

The output result is:

From the code above, we can see that the key value is retained when the key is a number, that is, the second parameter of array_reverse is true!

Sample code when the key is a character:

 "PHP 文"", "ID" => "www.php1.cn", "name1" => "Baidu", "name2" => "Sogou "); $ array = array_reverse ($ arr); // reverse order of the array to reverse the elements of the array print_r ($ arr); echo"
"; Print_r ($ array);?>

Output result:

When the array_reverse function reverses an array, the key value is retained when the key value is a string!

Note: When the array_reverse function is used, the original array is not affected and a new array is returned!

This article describes the reverse and unordered arrays. The next article describes the division, replacement, and truncation of arrays. For more information, see the interception of PHP arrays, equals and replacement of some arrays!

[Recommended tutorials]

  1. Related topic recommendations: php Array (Array)

  2. Related video course recommendations: other array functions: array_rand ()/array_sum ()/shuffle ()/range ()

The above is the detailed description of the PHP array random and Reverse order instances. For more information, see other related articles in the first PHP 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.