Introduction to the use of shuffle () and Array_rand () random functions in PHP array function sequences _php tips

Source: Internet
Author: User
Tags generator shuffle
Shuffle () Definition and usage
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.

Note: Since PHP 4.2.0, you no longer need to use the Srand () or Mt_srand () function to sow the random number generator, which is now automatically completed.

Grammar
Shuffle (array) parameter description
Array required. Specify the array to use.

Example
Copy Code code 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 an array and returns.

The second parameter is used to determine how many 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, the key of the original array is taken, regardless of whether a numeric or associative array is drawn, and placed in a new indexed array.

Note: Since PHP 4.2.0, you no longer need to use the Srand () or Mt_srand () function to sow the random number generator, which is now automatically completed.

Grammar
Array_rand (array,number) parameter description
Array required. Sets the input array parameters.
Number is optional. The default is 1. Specify how many random elements to return.

Example 1
Copy Code code as follows:

<?php
$a =array ("a" => "Dog", "B" => "Cat", "C" => "horse");
Print_r (Array_rand ($a, 1));
?>

Output:

B
Example 2
An array with a string key:
Copy Code code as follows:

<?php
$a =array ("a" => "Dog", "B" => "Cat", "C" => "horse");
Print_r (Array_rand ($a, 2));
?>

Output:

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

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.