Introduction to Shuffle and array_rand random function usage of PHP array function sequences

Source: Internet
Author: User
Tags shuffle
Shuffle () Definition and usage
The shuffle () function rearranges the elements in the array in random order.
Returns TRUE if successful, otherwise FALSE is returned.
Note: This function assigns the new key name to the cells in the array. This will delete the original key name and not just reorder it.
Note: Since PHP 4.2.0, it is no longer necessary to sow the random number generator with the Srand () or Mt_srand () function, which is now automatically completed.
Grammar
Shuffle (array) parameter description
Array required. Specifies the array to use.
Example

Copy the Code code 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 number of elements to select. If more than one element is selected, the array containing the random key name is returned, otherwise the key name of the element is returned.
Note: If the specified Array_rand () function extracts more than 1 indexes, either the numeric index array or the associative array is extracted, the original array key is taken and placed in a new indexed array.
Note: Since PHP 4.2.0, it is no longer necessary to sow the random number generator with the Srand () or Mt_srand () function, which is now automatically completed.
Grammar
Array_rand (array,number) parameter description
Array required. Specifies the input array parameters.
Number is optional. The default is 1. Specifies how many random elements are returned.
Example 1

Copy the Code code as follows:


$a =array ("a" = "Dog", "b" = "Cat", "C" and "Horse");
Print_r (Array_rand ($a, 1));
?>


Output:
B
Example 2
An array with a string key:

Copy the Code code as follows:


$a =array ("a" = "Dog", "b" = "Cat", "C" and "Horse");
Print_r (Array_rand ($a, 2));
?>


Output:
Array ([0] = c [1] = b)

The above describes the PHP array function sequence of shuffle and array_rand random function introduction, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.