Home  >  Article  >  Backend Development  >  PHP implements random array

PHP implements random array

WBOY
WBOYOriginal
2023-05-23 11:31:07621browse

PHP is a programming language widely used in web development and server-side programming. In PHP, an array is a very common data structure used to store a set of data. In some business scenarios, it is necessary to generate random arrays, which can improve the efficiency and reliability of the program to a certain extent. This article will introduce how to generate random arrays in PHP and some common application scenarios.

Generate a random array

In PHP, generating a random array is a very simple thing. Random numbers can be generated by using the rand() function and then stored in an array. The rand() function can accept two parameters: the first parameter represents the minimum value of the random number, and the second parameter represents the maximum value of the random number.

The following is a sample code:

$length = 10;
$min = 1;
$max = 100;
$arr = array();
for ($i = 0; $i < $length; $i++) {
    $arr[] = rand($min, $max);
}
print_r($arr);

This code will generate an array of length 10, where the value of each element is randomly generated between 1 and 100. Use the print_r() function to print the contents of the array:

Array ( [0] => 36 [1] => 45 [2] => 33 [3] => 100 [4] => 4 [5] => 26 [6] => 23 [7] => 38 [8] => 95 [9] => 77 )

In this way, most random array generation needs can be met. However, when you need to generate a more specific random array, you need to use some other techniques.

Generate an ordered random array

Sometimes, you need to generate an ordered random array. This is equivalent to changing the way of generating random numbers, and it is not difficult to implement. The generated random array can be sorted using the sort() function in PHP to generate an ordered random array.

The following is a sample code:

$length = 10;
$min = 1;
$max = 100;
$arr = array();
for ($i = 0; $i < $length; $i++) {
    $arr[] = rand($min, $max);
}
sort($arr);
print_r($arr);

This code will generate an ordered random array of length 10, where the value of each element is randomly generated between 1 and 100. Use the sort() function to sort the array:

Array ( [0] => 2 [1] => 9 [2] => 20 [3] => 41 [4] => 54 [5] => 59 [6] => 80 [7] => 84 [8] => 86 [9] => 91 )

Filter duplicate elements in the random array

When generating a random array, some duplicate elements often appear. If you need to filter out these duplicate elements, you can use the array_unique() function in PHP.

The following is a sample code:

$length = 10;
$min = 1;
$max = 100;
$arr = array();
for ($i = 0; $i < $length; $i++) {
    $arr[] = rand($min, $max);
}
$arr = array_unique($arr);
print_r($arr);

This code will generate a random array of length 10, where the value of each element is randomly generated between 1 and 100. Use the array_unique() function to filter out duplicate elements in the array:

Array ( [0] => 81 [1] => 24 [2] => 60 [3] => 89 [4] => 28 [5] => 31 [6] => 22 [7] => 38 [8] => 92 [9] => 85 )

Randomly extract array elements

Sometimes, you need to randomly extract several elements from an array. In PHP, you can use the array_rand() function to achieve this function. The array_rand() function can accept two parameters: the first parameter represents the array to be randomly sampled, and the second parameter represents the number of elements to be sampled.

The following is a sample code:

$arr = array('apple', 'banana', 'orange', 'grape', 'watermelon');
$random_keys = array_rand($arr, 3);
$random_items = array();
foreach ($random_keys as $key) {
    $random_items[] = $arr[$key];
}
print_r($random_items);

This code will randomly extract 3 elements from the array $arr and store them into the $random_items array:

Array ( [0] => apple [1] => banana [2] => grape )

Randomly shuffle the order of array elements

Sometimes, you need to randomly shuffle the elements in an array. In PHP, you can use the shuffle() function to achieve this function. The shuffle() function will shuffle the order of elements in the array and generate a random array.

The following is a sample code:

$arr = array('apple', 'banana', 'orange', 'grape', 'watermelon');
shuffle($arr);
print_r($arr);

This code will shuffle the order of elements in the array $arr:

Array ( [0] => watermelon [1] => orange [2] => grape [3] => banana [4] => apple )

Summary

In PHP , generating a random array is a very simple matter. You can use the rand() function and a loop statement to generate random numbers and store them in an array. Sometimes, you need to generate an ordered random array or filter out duplicate elements in an array. You can use the sort() function and the array_unique() function to achieve this. If you need to randomly select several elements from an array or randomly shuffle the order of array elements, you can use the array_rand() function and shuffle() function to achieve this. These techniques can help developers handle various business scenarios more efficiently.

The above is the detailed content of PHP implements random 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