When you do array
type-casting of json_decode
d value (with $assoc = false
), PHP creates an array with string indices:
$a = (array)json_decode('{"7":"value1","8":"value2","9":"value3","13":"value4"}');
var_export($a);
//array (
// '7' => 'value1',
// '8' => 'value2',
// '9' => 'value3',
// '13' => 'value4',
//)
And for some reason these indices are not accessible:
var_dump(isset($a[7]), isset($a['7']));
//false
//false
When you try to create the same array by PHP itself, it is being created with numeric indices (string are automatically converted), and values are accessible using both strings and numbers:
$c = array('7' => 'value1', '8' => 'value2', '9' => 'value3','10' => 'value4');
var_export($c);
var_dump(isset($c[7]), isset($c['7']));
//array (
// 7 => 'value1',
// 8 => 'value2',
// 9 => 'value3',
// 13 => 'value4',
//)
//
//true
//true
Does anybody know what is going on here? Is it some bug of older PHP versions (the issue seems to be fixed on PHP version >= 7.2, but I can't find anything related in changelog)?
Here's the demo of what is going on: https://3v4l.org/da9CJ.