Shuffle () and array_rand () Random Functions in PHP Array Function Sequence

Source: Internet
Author: User
Tags shuffle

Shuffle () Definition and usage
The shuffle () function sorts the elements in the array in random order.

If yes, true is returned. Otherwise, false is returned.

Note: This function assigns a new key name to the cell in the array. This will delete the original key name, not just the reorder.

Note: Since PHP 4.2.0, the random number generator is no longer needed to be planted using the srand () or mt_srand () function, which is now automatically completed.

Syntax
Shuffle (array) parameter description
Array is required. Specifies the array to be used.

ExampleCopyCodeThe Code is as follows: <? PHP
$ My_array = array ("A" => "dog", "B" => "cat", "C" => "horse ");
Shuffle ($ my_array); print_r ($ my_array );
?>

Output:

Array ([0] => CAT [1] => horse [2] => dog)

Array_rand () Definition and usage
The array_rand () function randomly selects one or more elements from the array and returns them.

The second parameter is used to determine the elements to be selected. If more than one element is selected, an array containing the random key name is returned. Otherwise, the key name of the element is returned.

Note: If the number of indexes extracted by the specified array_rand () function is greater than 1, the keys of the original array will be obtained no matter whether the index array or associated array is extracted, and put it in a new index array.

Note: Since PHP 4.2.0, the random number generator is no longer needed to be planted using the srand () or mt_srand () function, which is now automatically completed.

Syntax
Array_rand (array, number) parameter description
Array is required. Specify the input array parameters.
Number is optional. The default value is 1. Specifies how many random elements are returned.

Example 1Copy codeThe Code is as follows: <? PHP
$ A = array ("A" => "dog", "B" => "cat", "C" => "horse ");
Print_r (array_rand ($ A, 1 ));
?>

Output:

B
Example 2
Arrays with string keys:Copy codeThe Code is as follows: <? PHP
$ A = array ("A" => "dog", "B" => "cat", "C" => "horse ");
Print_r (array_rand ($ A, 2 ));
?>

Output:

Array ([0] => C [1] => B)

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.