array_rand() Example
Refreshing the screen should produce different results
(sometimes they'll be the same though since php's random
function isn't that good)
"John",
"Second"=>"Jane",
"Third"=>"Nick",
"Fourth"=>"Betty");
print "Original Array Values
";
// Print contents of the original array
print_r($arr);
// Create array of 4 randomly selected KEYS from the original array
$randKeys = array_rand($arr, 4);
// Print the array of the randomly selected KEYS
print "
Randomly selected keys array
";
print_r($randKeys);
print "
";
print "Using foreach loop
";
// Iterate through each element of $randKeys using the name $key
foreach ($randKeys as $key)
{
print $arr[$key] . "
";
}
print "
";
print "Using regular for loop
";
// More complicated version not using foreach
for ($x=0;$x<4; $x++)
{
print $arr[$randKeys[$x]] . "
";
}