Now here's an easy one for the PHP gurus... $mywords = "cat,dog,apple,banana,giraffe,nintendo,guava"; Code (markup): How can I create a new array, using 3 random values from $mywords? I'm guessing array_rand will come into play, but I just can't work it out...
first thats a string you have there, you can't use array rand with out doing something like $mywords = "cat,dog,apple,banana,giraffe,nintendo,guava"; $input = explode(',',$mywords); srand((float) microtime() * 10000000); $rand_keys = array_rand($input, 2); print_r($rand_keys); Code (markup): array_rand does not actually sort the array, it just returns some random keys to which can be used to pick random entry's from the array.
Sorry to be a complete dunce here... but $mywords = "cat,dog,apple,banana,giraffe,nintendo,guava"; $input = explode(',',$mywords); $rand_keys = array_rand($input, 2); print_r($rand_keys); Code (markup): is printing Array ( [0] => 1 [1] => 2 ) How can I make it output the actual words (e.g banana, guava etc)?
$mywords = "cat,dog,apple,banana,giraffe,nintendo,guava"; $words = explode(',', $mywords); $keys = array_rand($words, 3); foreach ($keys AS $key) { echo $words[$key], ' '; } PHP: