Reputation: 3796
Long time, my PHP-application ran on a linux server with apache and PHP.
Now I've set up a windows server with apache and php and simple PHP-Programs have problems.
var_dump($data);
die(var_dump($data['a']));
results in
object(stdClass)#1 (1) { ["a"]=> string(1) "b" }
Fatal error: Cannot use object of type stdClass as array in BLUB.php on line 14
var_dump says there is an index a. Why does $data['a'] throw an exception?
EDIT: $data is json_decode($something);
Upvotes: 4
Views: 2099
Reputation: 2229
since $data
in an object you have to access it this way:
$data->a;
Otherwise, you can typecast it to an array and access it actually like an array
$dataArr = (array) $data;
$dataArr['a'];
NOTE
This is possible only whether your object attributes are public
Upvotes: 0
Reputation: 1552
You should use:
$data = json_decode($something, true);
To get array from json
Upvotes: 2
Reputation: 1186
The error contains your answer - $data is actually an object of type stdClass. You could either:
a) access the property 'a' with $data->a or b) typecast $data as an array using (array)$data
Upvotes: 3
Reputation:
As the error says $data
is an object, not an array. As such, you want to use object access, rather than array access. In this case, that would be var_dump($data->a)
Upvotes: 2
Reputation: 3953
Because its an object, not an array. You cant access it like that. This would be correct:
$data->a;
Upvotes: 3