Detailed explanation of the steps to use the php array function shuffle() and array_rand() random functions

php中世界最好的语言
Release: 2023-03-27 07:08:02
Original
1838 people have browsed it

This time I will bring you php arrayfunctionshuffle() and array_rand() detailed explanation of the steps to use the random function, php array function shuffle() and What are the precautions when using array_rand() random function? The following is a practical case, let’s take a look.

1, shuffle() definition and usage
shuffle() function rearranges the elements in the array in random order.

If successful, return TRUE, otherwise return FALSE.

Note: This function assigns a new key name to the unit in the array. This will delete the original keys rather than just reorder them.

Note: As of PHP 4.2.0, it is no longer necessary to seed the random number generator with the srand() or mt_srand() function, it is now automatically done.

Syntax
shuffle(array) Parameter Description
array Required. Specifies the array to use.

<?php 
$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse"); 
shuffle($my_array); print_r($my_array); 
?>
Copy after login

Output:

Array ( [0] => Cat [1] => Horse [2] => Dog )
Copy after login

2, array_rand() definition and usage
array_rand() function randomly selects one or more elements from the array and returns.

The second parameter is used to determine how many elements to select. If more than one element is selected, an array containing a random key is returned, otherwise the key of the element is returned.

Note: If the number of indexes extracted by the specified array_rand() function is greater than 1, then regardless of whether it is a numeric index array or an associative array, the key of the original array will be obtained and placed in a new index array. middle.

Note: As of PHP 4.2.0, it is no longer necessary to seed the random number generator with the srand() or mt_srand() function, it is now done automatically.

Syntax
array_rand(array,number) Parameter Description
array Required. Specifies the input array parameters.
number Optional. The default is 1. Specifies how many random elements to return.

<?php 
$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse"); 
print_r(array_rand($a,1)); 
?>
Copy after login

Output:
b

Example 2, array with string keys:

<?php 
$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse"); 
print_r(array_rand($a,2)); 
?>
Copy after login

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

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

array_search() function returns the key name by element value Detailed explanation of the steps

php array search function usage Summary of methods

The above is the detailed content of Detailed explanation of the steps to use the php array function shuffle() and array_rand() random functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!