Shuffle () and array_rand () random functions in php array function sequence _ PHP Tutorial

Source: Internet
Author: User
Tags shuffle
Introduction to shuffle () and array_rand () random functions in php array function sequence. Shuffle () definition and usage shuffle () function sorts the elements in the array in random order. If yes, TRUE is returned. otherwise, FALSE is returned. Note: This function is a unit in an array. 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.

Example

The code is as follows:


$ 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 1

The code is as follows:


$ A = array ("a" => "Dog", "B" => "Cat", "c" => "Horse ");
Print_r (array_rand ($ a, 1 ));
?>


Output:

B
Example 2
Arrays with string keys:

The code is as follows:


$ A = array ("a" => "Dog", "B" => "Cat", "c" => "Horse ");
Print_r (array_rand ($ a, 2 ));
?>


Output:

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

The http://www.bkjia.com/PHPjc/324498.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/324498.htmlTechArticleshuffle () definition and usage shuffle () function sorts the elements in the array in random order. If yes, TRUE is returned. otherwise, FALSE is returned. Note: This function is a unit in the array...

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.