How does php obtain the random array list? php user-defined functions get random arrays
- Function create_random_ids ($ min, $ max, $ limited)
- {
- $ _ Base_ids = range ($ min, $ max );
- $ _ Temp_key = array_rand ($ _ base_ids, min (count ($ _ base_ids), $ limited + 10 ));
- // Splicing
- $ Ids = array ();
- For ($ x = 0; $ x <count ($ _ temp_key); $ x ++ ){
- $ Ids [] = $ _ base_ids [$ _ temp_key [$ x];
- }
- Return $ ids;
- }
II. usage of shuffle () and array_rand () in php arrays for random extractionIn php, php arrays are randomly extracted and can be completed using shuffle () and array_rand () functions. Random extraction means that the elements in the original array are output after they are disrupted. after each execution, the extraction sequence or elements are different. This function can be used to display different advertisements on the webpage with shuffle () function to achieve random array extraction:
- $ TextArray = array ('1', '2', '3', '4', '5', '6', '7 ');
- Shuffle ($ textArray );
- Print_r ($ textArray );
- ?>
-
Result: array ([0] => 6 [1] => 3 [2] => 7 [3] => 4 [4] => 1 [5] => 2 [6] => 5) implement random sorting of array elements; php also provides a function to randomly extract values from the array: array_rand (). The Call format is as follows: array_rand ( <数组> , [Number of elements to be extracted]);
- $ Arry = array ('A', 'B', 'C', 'D ');
- $ Result = array_rand ($ arry, 2 );
- Foreach ($ result as $ val ){
- Echo $ arry ["$ val"]. "";}
- ?>
-
Result: the BC refresh results are different; |