Home  >  Article  >  Backend Development  >  How to use array_rand function in PHP to randomly obtain multiple elements in an array

How to use array_rand function in PHP to randomly obtain multiple elements in an array

PHPz
PHPzOriginal
2023-06-26 13:46:421004browse

The array_rand function in PHP is a very practical function that can help us randomly obtain elements in the array. In actual development, we may often use this function to obtain a single element in an array, but in fact it can also obtain multiple elements.

When using the array_rand function, we need to pass in two parameters: the array and the number of random elements to be obtained. For example, we have an array containing 10 elements, and we need to randomly obtain 3 elements. You can use the following code:

$arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$rand_keys = array_rand($arr, 3);

In this example, $rand_keys is an array containing 3 random key names. Array, these three key names correspond to the key names of the three selected elements in the $arr array.

We can use these key names to get the corresponding element value, for example:

echo $arr[$rand_keys[0]]; // 随机获取到的第一个元素
echo $arr[$rand_keys[1]]; // 随机获取到的第二个元素
echo $arr[$rand_keys[2]]; // 随机获取到的第三个元素

If we want to get a random element in a nested array containing multiple elements, we can also use array_rand function. For example, we have the following nested array:

$arr = [
    ['name' => '小明', 'age' => 18],
    ['name' => '小红', 'age' => 19],
    ['name' => '小刚', 'age' => 20],
    ['name' => '小丽', 'age' => 21],
];

If we need to randomly get two elements in it, we can use the following code:

$rand_keys = array_rand($arr, 2);
$rand_arr = [];
foreach ($rand_keys as $key) {
    $rand_arr[] = $arr[$key];
}

In the above example, $rand_keys is a An array containing two random key names. Use foreach to traverse $rand_keys, obtain the element value of the corresponding key name, and form a new array $rand_arr, which contains two randomly selected elements.

print_r($rand_arr);
// 输出结果可能为:
// Array (
//  [0] => Array ( [name] => 小刚 [age] => 20 )
//  [1] => Array ( [name] => 小明 [age] => 18 )
// )

To sum up, using the array_rand function in PHP to randomly obtain multiple elements in an array can be achieved by passing in the number of random elements to be obtained. The obtained random elements correspond to the key names in the original array, and we can obtain the corresponding element values ​​through these key names. If it is a nested array, we can use the random key name obtained by foreach traversal to obtain the corresponding element value and form a new array.

The above is the detailed content of How to use array_rand function in PHP to randomly obtain multiple elements in an array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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