Home > Article > Backend Development > Randomly extract some elements from a PHP array_PHP tutorial
The function of randomly extracting some elements is very simple. Just use array_rand and range. If you randomly select an element, just use mt_rand and grow it into a random number whose length does not exceed the array length.
I will provide you with several methods below for your reference.
Method-:
The code is as follows
|
Copy code
|
||||||||
$arr = range(1,10,1);
$newArr = array_flip($newarr); //Key and value interchange $arr3 = array_diff_key($arr,$newArr); //Get the ones with the same key
$arr1 = array_diff_key($arr,$arr3); //Get the ones with the same key |
print_r($arr1);