PHP array with numeric keys as string can't be used

Viewed 12001

I have a PHP array that has numeric keys as a string type.

But when I try and access them PHP is giving me an undefined index error.

$a = (array)json_decode('{"1":1,"2":2}');
var_dump($a);
var_dump(isset($a[1]));
var_dump(isset($a["1"]));
var_dump($a[1]);
var_dump($a["1"]);

Output:

array (size=2)
    '1' => int 1
    '2' => int 2

boolean false

boolean false

ERROR: E_NOTICE: Undefined offset: 1

null

ERROR: E_NOTICE: Undefined offset: 1

null

How do I access these values?

Demo: http://codepad.viper-7.com/8O03IM

6 Answers
Related