0

I have below json data and decode it to display on the screen. When I check the type of the value, it shows array instead of object. How to get actual type of value in PHP.

JSON is

{ "allData" : { "image" : [], "contents": {.., "box": {}, "text":[]} } }

When I decode and parse the above JSON data the "allData", "contents", "box" type are shows as array instead of object. How can I get those type as object and "image" type as array. Please help.

Thanks, Guru

2
  • 1
    Can you show the code you are using and the expected output you want? Commented Dec 17, 2018 at 8:55
  • Hi Jerodev, Thanks for your quick reply. I'm using test json like this. { "allData" : { "image" : ["img1.png"], "contents": {"title":"title name", "box": {"name":["sample text 1","sample text2"]}, "text":[]} } }. My expectation is, when I parse using foreach($data as $key=>$val){ gettype($val) } it should give me allData is object, image is array, contents is object, box is object and name is array like that. Commented Dec 17, 2018 at 9:05

3 Answers 3

1

This normally occurs when you are using the true option in the json_decode function.

For eg.,

$str = '{"allData":{"image":["img1.png"],"contents":{"title":"title name","box":{"name":["sample text 1","sample text2"]},"text":[]}}}';
var_dump(json_decode($str, true));

Just try to remove the true in the json_decode function and you should get an object.

Hope this helps.

Sign up to request clarification or add additional context in comments.

10 Comments

Hi Kishen, I tried as you said. Means I removed true from the json_decode($str); but it throw error like this -> Fatal error: Uncaught Error: Cannot use object of type stdClass as array in....
Can you post the complete JSON String please ?
One more thing: you can use foreach on arrays not on objects. if you want to use foreach loop then you'll have to convert your JSON to array.
Hi Kishen, Thanks quick reply. My JSON file has this data -> { "allData" : { "image" : ["img1.png"], "contents": {"title":"title name", "box": {"name":["sample text 1","sample text2"]}, "text":[["title","desc text"],[["title","desc text"]]]} } }. My expectation is, when I parse using foreach($data as $key=>$val){ gettype($val) } it should give me allData is object, image is array, contents is object, box is object and name is array like that.
@Guru — PHP doesn't really distinguish between associative arrays and numerically index arrays. If you want to have objects and not associative arrays, then you have to treat them like objects (which, as Krishen said, means you can't use foreach like that).
|
0

If you use json_decode with the true option, it will return the result as an array.

Maybe you can check this link.

If you get "Cannot use object of type stdClass as array" the error, you can look at this answer.

<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

var_dump(json_decode($json));
var_dump(json_decode($json, true));

?>

1 Comment

Hi Cagatay, Thanks for your quick reply. Let me check in this.
0

Extract from the RFC 7159 (JSON) : These are the six structural characters:

  begin-array     = ws %x5B ws  ; [ left square bracket

  begin-object    = ws %x7B ws  ; { left curly bracket

  end-array       = ws %x5D ws  ; ] right square bracket

  end-object      = ws %x7D ws  ; } right curly bracket

..

However: php allows you to treat the result as an array (of arrays)

so:

json_decode($json, true); // return as array

returns the result as an array.

and

json_decode($json)

gives you the result as Objects AND arrays . So given your example:

"allData" : { "image" : [], ..

returns a stdClass-Object with the field "image" of type array. The array is empty for your example.

So to retrieve all images, use something like:

$result=json_decode($json);

foreach($result->allData->image as $img) {
  echo "found image: $img.";
}

1 Comment

Hi Michael. Thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.