Right now you basically do this:
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand_keys = array_rand($input, 2); //Array ( [0] => Random Key 1 [1] => Random Key 2 )
Foreach loop:
foreach($rand_keys as $value) {
print $value;
}
╔═══════════════╦══════════════╗
║ Iteration Nr. ║ $value ║
╠═══════════════╬══════════════╣
║ 1 ║ Random Key 1 ║
║ 2 ║ Random Key 2 ║
╚═══════════════╩══════════════╝
For loop:
$length = count($rand_keys);
for ($i = 0; $i < $length; $i++) {
print $rand_keys[$i];
}
╔═══════════════╦════╦════════════════╦═════════╗
║ Iteration Nr. ║ $i ║ $rand_keys[$i] ║ $length ║
╠═══════════════╬════╬════════════════╬═════════╣
║ 1 ║ 0 ║ Random Key 1 ║ 2 ║
║ 2 ║ 1 ║ Random Key 2 ║ 2 ║
╚═══════════════╩════╩════════════════╩═════════╝
So array_rand()
just returns random keys. Use that returned keys to access elements in your input array:
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand_keys = array_rand($input, 2);
foreach($rand_keys as $key) {
echo $input[$key];
}