Home  >  Article  >  Backend Development  >  How to randomly extract several elements from a php array (three methods)

How to randomly extract several elements from a php array (three methods)

PHPz
PHPzOriginal
2023-04-12 09:18:572686browse

In PHP development, we often need to randomly extract several elements from an array. Sometimes, we need to randomly select a few lucky users from a user list to give gifts. Or, we need to randomly select several dishes from a menu array as recommended dishes. This article will introduce how to randomly extract several elements from an array in PHP.

Method 1: Use shuffle and array_slice functions

The shuffle function can disrupt the elements of the array and randomly rearrange each element in the array. Then, use the array_slice function to extract a certain number of elements from the array. This method is simple and easy to understand and suitable for small arrays.

The code is as follows:

<?php
$array = array(&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;, &#39;watermelon&#39;, &#39;grape&#39;, &#39;pear&#39;);
shuffle($array);
$rand_keys = array_rand($array, 3);
$result = array_slice($array, $rand_keys[0], 3);
print_r($result);
?>

Analysis:

  1. Define an array $array, containing 6 elements.
  2. Use the shuffle function to randomly sort the array elements.
  3. Use the array_rand function to randomly extract the index positions of 3 elements from the scrambled array.
  4. Use the array_slice function to get 3 random elements from the array.
  5. Finally print out the results.

Method 2: Use the array_rand function

The array_rand function can directly return the index of one or more random elements from the array. This method works for large arrays.

The code is as follows:

<?php
$array = array(&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;, &#39;watermelon&#39;, &#39;grape&#39;, &#39;pear&#39;);
$rand_keys = array_rand($array, 3);
$result = array();
foreach ($rand_keys as $key) {
    $result[] = $array[$key];
}
print_r($result);
?>

Analysis:

  1. Define an array $array, containing 6 elements.
  2. Use the array_rand function to retrieve the index positions of 3 random elements from the array.
  3. Use a foreach loop to add randomly retrieved elements to the result array.
  4. Finally print out the results.

Method 3: Use the mt_rand function

The mt_rand function can return a random integer within a specified range, so it can be used to generate random array keys. This method is relatively inefficient and suitable for small arrays.

The code is as follows:

<?php
$array = array(&#39;apple&#39;, &#39;banana&#39;, &#39;orange&#39;, &#39;watermelon&#39;, &#39;grape&#39;, &#39;pear&#39;);
$keys = array_keys($array);
$total = count($keys);
$pick_num = 3;
$rand_keys = array();
$result = array();
while (count($rand_keys) < $pick_num) {
    $rand_keys[] = $keys[mt_rand(0, $total-1)];
}
foreach ($rand_keys as $key) {
    $result[] = $array[$key];
}
print_r($result);
?>

Analysis:

  1. Define an array $array, containing 6 elements.
  2. Use the array_keys function to get the array $key, which is the collection of keys.
  3. Get the total length of the array $total.
  4. Define variable $pick_num, indicating the number of elements to be taken out.
  5. Define a $rand_keys array to store randomly retrieved keys.
  6. Use a loop to generate actual random keys. When the number of $rand_keys reaches $pick_num, break out of the loop.
  7. Use a foreach loop to add randomly retrieved elements to the result array.
  8. Finally print out the results.

The above are three ways to randomly select several elements from a PHP array. Different methods can be selected according to the actual situation. For large arrays, the array_rand function can be selected, etc. This operation seems simple, but it can improve the product experience, enhance the fun, and enrich the user experience.

The above is the detailed content of How to randomly extract several elements from a php array (three methods). 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