This question has already been answered:

Good day! Tell me why the cycle displayed 1 extra element

$arr = array("name" => "Маша","name2" => "Паша","Генадий","Валера", "кофе", "чай","8 утра"); foreach($arr as $key => $elem){ if($key == "name"){ echo $key." ".$elem . "<br>"; } 

}

displays: name Masha 0 Genadiy

Reported as a duplicate by Visman , Dmitriy Simushev , Cheg , andreymal , Ipatiev September 16 Sep '17 at 13:35 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

1 answer 1

In case you compare a number with a string or two strings containing numbers, each string will be converted to a number, and they will be compared as numbers. These rules also apply to the switch statement. Type conversion does not occur when using === or! == since in this case, in addition to the values ​​themselves, types are also compared.

therefore

  var_dump(0 == "a"); // 0 == 0 -> true var_dump("1" == "01"); // 1 == 1 -> true var_dump("10" == "1e1"); // 10 == 10 -> true var_dump(100 == "1e2"); // 100 == 100 -> true 

In your case, a comparison occurred 0 == 'name'