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

Joseph_J
  • 3,654
  • 2
  • 13
  • 22
Guru
  • 13
  • 6
  • 1
    Can you show the code you are using and the expected output you want? – Jerodev Dec 17 '18 at 08: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. – Guru Dec 17 '18 at 09:05

3 Answers3

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.

Kishen Nagaraju
  • 2,062
  • 9
  • 17
  • 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.... – Guru Dec 17 '18 at 09:09
  • Can you post the complete JSON String please ? – Kishen Nagaraju Dec 17 '18 at 09:10
  • 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. – Kishen Nagaraju Dec 17 '18 at 09:14
  • 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 Dec 17 '18 at 09:17
  • 1
    @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). – Quentin Dec 17 '18 at 09:22
  • Hi Quentin, Thanks for your answer. I'm sorry I'm new to PHP. My case, I don't know how the JSON file will be and what are all the keys and how many items are in the JSON. I need to just decode the JSON data and let the user to modify and save it. When the user clicked on the Save button I need to re-build JSON structure as it was originally and need to save as in the same location. While trying it, I was getting all are array and can n't give '{' and '}' if object is. Because I'm getting all are array. How to fix this? Please help me. :) – Guru Dec 17 '18 at 09:34
  • @Guru - If you are looking for that answer then you need to post the appropriate code here so that we can have a look at it and give you proper solutions. I have given you the solution that you had highlighted for in the description. Please update the question. – Kishen Nagaraju Dec 17 '18 at 09:38
  • Hi Kishen, I'm unable to post all code here. Is there any way to attach file here? – Guru Dec 17 '18 at 10:05
  • Hi Kishen, It's working fine now without true in json_decode(). – Guru Dec 18 '18 at 04:44
  • Thanks a lot all of you. I understand the concept. :) – Guru Dec 18 '18 at 04:45
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));

?>
Cagatay
  • 21
  • 2
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.";
}
michael - mlc
  • 376
  • 5
  • 5