Php randomly selects several non-repeating elements from the array _ PHP Tutorial

Source: Internet
Author: User
Php randomly selects several non-repeating elements from the array. Php randomly selects several non-repeating elements from the array. This article describes how php randomly selects several non-repeating elements from the array. Share it with you for your reference. The specific implementation of php random selection of several non-repeating elements from the array method

This example describes how php randomly selects non-repeating elements from an array. Share it with you for your reference. The specific implementation method is as follows:

The code is as follows:

/*
* $ Array = the array to be filtered
* $ Total = the maximum number of items to return
* $ Unique = whether or not to remove duplicates before getting a random list
*/
Function unique_array ($ array, $ total, $ unique = true ){
$ NewArray = array ();
If (bool) $ unique ){
$ Array = array_unique ($ array );
}
Shuffle ($ array );
$ Length = count ($ array );
For ($ I = 0; $ I <$ total; $ I ++ ){
If ($ I <$ length ){
$ NewArray [] = $ array [$ I];
}
}
Return $ newArray;
}
$ Phrases = array ('Hello sailor', 'Acid Test', 'Bear Garden ', 'botch A job', 'Dark Horse ',
'In The red', 'man up', 'pan out', 'quid Pro quo', 'rub It in', 'turncoat ',
'Yes Man ', 'all wet', 'bag Lady', 'bean Feast ', 'Big wigb', 'Big wigb', 'Bear Garden'
, 'All wet', 'quid Pro quo', 'rub It in ');
Print_r (unique_array ($ phrases, 1 ));
// Returns 1 result
Print_r (unique_array ($ phrases, 5 ));
// Returns 5 unique results
Print_r (unique_array ($ phrases, 5, false ));
// Returns 5 results, but may have duplicates if
// There are duplicates in original array
Print_r (unique_array ($ PHP, 100 ));
// Returns 100 unique results
Print_r (unique_array ($ phrases, 100, false ));
// Returns 100 results, but may have duplicates if
// There are duplicates in original array

I hope this article will help you with php programming.

Examples in this article describes how php randomly selects a number of non-repeating elements from an array. Share it with you for your reference. Specific implementation...

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.