After years of confusion I am trying to get to grips with PHP arrays and especially in combination with foreach
loops.
There are various similar questions but I am really trying to understand why this is NOT working rather than make it work per se (will be using lots of foreach
loops and arrays in more complicated stuff shortly).
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"37");
Have a simple foreach loop:
foreach ($age as $ages )
{
n();n();
echo($ages);
n();
$v="volvo";
$key = key($age);
if ($age["Ben"]==$ages)
{
echo "<BR><BR>Result is $key is $ages <BR>";
}
}
Output is:
35
37
Result is Ben is 37
37
Result is Ben is 37
All fine but I was expecting the last name to be Joe.
I thought that a foreach
looped through each array value pair as a KEY VALUE
pair. So why am I getting Ben twice?