I have an array and PHP and when I print it out I can see the values I need to access, but when I try accessing them by their key I am getting a PHP Notice. I printed the array with print_r:
Array
(
[207] => sdf
[210] => sdf
)
When I try to access the array using the index I get an undefined offset notice. Here is my code:
print_r($output);
echo $output[207]; // Undefined Offset
echo $output["207"]; // Undefined Offset
The $output
array is the result of a call to array_diff_key and is input originally as JSON through an HTTP POST request.
array_keys gives me the following:
Array
(
[0] => 207
[1] => 210
)
In response to the comments:
var_dump(key($output));
outputs:
string(3) "207"
var_dump(isset($output[key($output)]));
outputs:
bool(false)
See this section on converting an object to an array in the PHP Manual:
When converting to an array from an object in PHP, integer array keys are stored internally as strings. When you access array elements in PHP or use an array normally, keys that contain valid integers will be converted to integers automatically. An integer stored internally as a string is an inaccessible key.
Note the difference:
print_r on both those arrays gives identical output, but with var_dump you can see the differences.
Here is some code that reproduces your exact problem:
And the simple fix is to pass in
true
to json_decode for the optionalassoc
argument, to specify that you want an array not an object:The problem arises when casting to
array
an object that has string keys that are valid integers.If you have this object:
and you cast it with
you get this array
which has keys that can only be accessed by looping through them, since a direct access like
$array["207"]
will always be converted to$array[207]
, which does not exist.Since you are getting an object like the one above from
json_decode()
applied to a string likeThe best solution would be to avoid numeric keys in the first place. These are probably better modelled as numeric properties of an array of objects:
This data structure has several advantages over the present one:
If a property → object map is needed, it can be quickly obtained, e.g., like this:
The other solution would be to do as ascii-lime suggested: force
json_decode()
to output associative arrays instead of objects, by setting its second parameter totrue
:For your input data this produces directly
Note that the keys of the array are now integers instead of strings.
I would consider changing the JSON data structure a much cleaner solution, though, although I understand that it might not be possible to do so.
I've just found this bug which causes array elements to be inaccessible sometimes in PHP when the array is created by a call to unserialize.
Create a test PHP file containing (or run from the command line) the following script:
If you get errors you have a version of PHP with this bug in it and I recommend upgrading to PHP 5.3
Try use my approach:
Try
to learn more on what is happening.
What exact line/statement is throwing you a warning?
Just put error_reporting(0); in you method or at start of file. It will solved your issue.