Array keys and case sensitivity

March 29th, 2005. Tagged: PHP certification

Yep, array keys are case sensitive.
So you can do:
< ?php $a = array('key'=> 1, 'Key' => 2);
print_r($a);
?>and it will print

Array ( [key] => 1 [Key] => 2 )

Similarly if you call array_keys() on $a, like:
< ?php $a = array('key'=> 1, 'Key' => 2);
print_r(array_keys($a));
?>...you'll get:

Array ( [0] => key [1] => Key )

Defining the same key in an array (even in the same array declaration) won't cause an error, but will result in the latest value taking precedence. Like:
< ?php $a = array('key'=> 1, 'key' => 2);
print_r($a);
?>will output:

Array ( [key] => 2 )

Tell your friends about this post on Facebook and Twitter

Sorry, comments disabled and hidden due to excessive spam.

Meanwhile, hit me up on twitter @stoyanstefanov