Random extraction of some elements of this function is very simple, as long as the use of Array_rand and range can be achieved, if the arbitrary extraction of an element directly using Mt_rand and then grow into a random number length does not exceed the length of the array.
Next I will give you a few ways for everyone to refer to.
Method-:
| The code is as follows |
Copy Code |
$arr = range (1,10,1);
$newarr = Array_rand ($arr, 6); Randomly get keys in 6 arrays
$NEWARR = Array_flip ($newarr); Key and Value Interchange
$arr 3 = Array_diff_key ($arr, $NEWARR); Take the same key
$arr 1 = Array_diff_key ($arr, $arr 3); Take the same key
Print_r ($arr 1); |
Result: Array ([0] = 1 [1] = 2 [2] = 3 [3] = 4 [5] = 6 [9] = 10)
Method Two:
| The code is as follows |
Copy Code |
$arr = range (1,10,1);
$newarr = Array_rand ($arr, 6); Randomly get keys in 6 arrays
$ArrNew = Array ();
foreach ($newarr as $k = $v) { $ArrNew [$v] = $arr [$v]; }
Print_r ($ARRNEW); |
Result: Array ([1] = 2 [2] = 3 [3] = 4 [4] = 5 [6] = 7 [7] = 8)
Method Three: This method does not retain the key name, for everyone to refer to.
| The code is as follows |
Copy Code |
$arr = range (1,10,1);
Shuffle ($arr); Scrambled Array
$newarr = Array_splice ($arr, 0,6);
Print_r ($newarr); |
Result: Array ([0] = 7 [1] = 4 [2] = 2 [3] = [4] = 9 [5] = 6)
http://www.bkjia.com/PHPjc/628721.html www.bkjia.com true http://www.bkjia.com/PHPjc/628721.html techarticle random extraction of some elements this method of implementation is very simple, as long as the use of Array_rand and range can be achieved, if the arbitrary extraction of an element directly using Mt_rand and then grow into a random number ...