How does php obtain the random array list? php user-defined functions get random arrays

Source: Internet
Author: User
How does php obtain the random array list? php user-defined functions get random arrays

  1. Function create_random_ids ($ min, $ max, $ limited)
  2. {
  3. $ _ Base_ids = range ($ min, $ max );
  4. $ _ Temp_key = array_rand ($ _ base_ids, min (count ($ _ base_ids), $ limited + 10 ));
  5. // Splicing
  6. $ Ids = array ();
  7. For ($ x = 0; $ x <count ($ _ temp_key); $ x ++ ){
  8. $ Ids [] = $ _ base_ids [$ _ temp_key [$ x];
  9. }
  10. Return $ ids;
  11. }

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:

  1. $ TextArray = array ('1', '2', '3', '4', '5', '6', '7 ');
  2. Shuffle ($ textArray );
  3. Print_r ($ textArray );
  4. ?>

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]);

  1. $ Arry = array ('A', 'B', 'C', 'D ');
  2. $ Result = array_rand ($ arry, 2 );
  3. Foreach ($ result as $ val ){
  4. Echo $ arry ["$ val"]. "";}
  5. ?>

Result: the BC refresh results are different;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.