23.9.1. ProblemYou want to randomly reorder all lines in a file. You have a file of funny quotes, for example, and you want to pick out one at random. 23.9.2. SolutionRead all the lines in the file into an array with file( ) , and then shuffle the elements of the array, as in Example 23-27. Randomizing all lines in a file
23.9.3. DiscussionThe shuffle( ) function randomly reorders the array elements, so after shuffling, you can pick out $lines[0] as a quote to display. 23.9.4. See AlsoRecipe 4.20 for shuffle( ); documentation on shuffle( ) at http://www.php.net/shuffle. |