开发者

cannot use object of type stdClass as array when trying to modify a array

I've just read a simple array from my db that was json_encoded, so i decoded it and tried to change a value at index 0, but i get the following error

can开发者_JS百科not use object of type stdClass as array when trying to modify a array

any help?


by default json strings are decoded as objects and not arrays. so you must use json_decode's second parameter. so your call should be like this:

$array = json_decode($string, true);


A JSON-encoded entity can be an object or an array - it appears that in your example it's an object (of type stdClass). You can either reference it as an object

$jsonProperty = $decoded->property;

save the original JSON object as an array

$encodedJson = json_encode((array) $object);

or force decoding as an associative array

$decodedJson = json_decode($json, true);

Personally I'd use the first option and refer to a JSON object with PHP's object access notation for clarity.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜