Php code that randomly extracts some elements from the array
-
/**
- * Random array element extraction
- * Author: notuser
- * 2012-12-29
- */
- Class getValues {
- Public function inputValue ($ inputArray ){
- $ This-> inputArray = $ inputArray;
- }
- Public function getValue ($ number ){
- $ This-> number = $ number;
- For ($ I = 0; $ I <$ this-> number; $ I ++ ){
- $ Index = rand (0, count ($ this-> inputArray)-1-$ I );
- $ GetArray [$ I] = $ this-> inputArray [$ index];
- Unset ($ this-> inputArray [$ index]);
- For ($ k = $ index; $ k <count ($ this-> inputArray)-1; $ k ++ ){
- $ This-> inputArray [$ k] = $ this-> inputArray [$ k + 1];
- }
- }
- // Asort ($ getArray); // sorts data from small to large and can be modified as needed
- Return $ getArray;
- }
- }
-
// Test code
- $ Keywords = array (
- "We ",
- "You ",
- "They"
- );
- $ GetValue = new getValues ();
- $ GetValue-> inputValue ($ keywords );
- $ Key = $ getValue-> getValue (1); // randomly selects an element from the array.
- Echo $ key;
- ?>
|