Could someone please explain to me how the count function works with arrays like the one below?
My thought would be the following code to output 4, cause there are 4 elements there:
$a = array
(
"1" => "A",
1=> "B",
"C",
2 =>"D"
);
echo count($a);
countworks exactly as you would expect, e.g. it counts all the elements in an array (or object). But your assumption about the array containing four elements is wrong:1 => "B"will overwrite"1" => "A".2 => "C"2 => "D"you overwrote “C”.So your array will only contain
1 => "B"and2 => "D"and that’s whycountgives 2. You can verify this is true by doingprint_r($a). This will givePlease go through http://www.php.net/manual/en/language.types.array.php again.