strange foreach loop output
Posted: Tue Oct 12, 2010 11:08 am
Hi!
This bit of code does exactly what I want it to just not sure why.
The way I would (***_u_me) this code to be written would be to replace the
echo ($array_2[$index]); with echo ($array_2[$value]); to get the output I'm
getting. Basically the foreach loop loops through $array_1 when it finds a key with a null value it outputs the $value or the $index which ever I decide to output of $array_2 which corresponds to that value. when I echo ($array_2[$value]) I get no output and when I echo ($array_2[$index]) I get the wanted output below. It doesn't seem right
output
Matt
Jessica
Thanks,
Steadythecourse
This bit of code does exactly what I want it to just not sure why.
The way I would (***_u_me) this code to be written would be to replace the
echo ($array_2[$index]); with echo ($array_2[$value]); to get the output I'm
getting. Basically the foreach loop loops through $array_1 when it finds a key with a null value it outputs the $value or the $index which ever I decide to output of $array_2 which corresponds to that value. when I echo ($array_2[$value]) I get no output and when I echo ($array_2[$index]) I get the wanted output below. It doesn't seem right
Code: Select all
<?php
function test() {
$array_1 = array("Matt" => NULL, "Kim" => 1, "Jessica" => NULL, "Keri" => 1);
$array_2 = array("Matt","Kim","Jessica","Keri");
foreach ($array_2 as $index => $value) {
if (!isset($array_1[$value])) {
echo ($array_2[$index]);
echo "<br />";
}
}
}
test();
Matt
Jessica
Thanks,
Steadythecourse