9

I am creating an very large multidimensional array using PHP. Each object contains Name, ID, ParentID and Children. Children is an array of more objects in the same format.

It is critical I name the IDs of each object - this helps me put each object under the correct parent. (In the code below, I use 101, 102 etc.)

However, the problem I am having is when I return the array in JSON using json_encode. Each 'Children' array is being printed as an object, not an array - as shown in the JSON code below.

As I read on another SO thread here, they "are made as objects because of the inclusion of string keys" - although they are numbers, they are still strings.

{
"101": {
 "ID": "101",
 "ParentID": "0",
 "Name": "Root One"
 "Children": {
 "102": {
 "ID": "102",
 "ParentID": "101",
 "Name": "Child One"
 },
 "103": {
 "ID": "103",
 "ParentID": "101",
 "Name": "Child Two",
 "Children": {
 "104": {
 "ID": "104",
 "ParentID": "103",
 "Name": "Child Child One"
 }
 }
 },

Does anyone know how to overcome this issue?

Edit: The JSON should look like this (the square brackets are important!):

[
{
 "ID": "101",
 "ParentID": "0",
 "Name": "Root One",
 "Children": [
 {
 "ID": "102",
 "ParentID": "101",
 "Name": "Child One",
 "Children": [
asked May 7, 2013 at 12:59
2
  • It's actually because the indices are not contiguous, i.e. there are gaps. Commented May 7, 2013 at 13:04
  • Also, your "children" ARE objects (from my perspective) and not arrays. Be glad, and let OOP flow within you. Commented May 7, 2013 at 13:20

4 Answers 4

6

A JSON array has no explicit indexes, it's just an ordered list. The only JSON data structure that has named keys is an object. The literal should make this quite obvious:

["foo", "bar", "baz"]

This array has no named indices and there isn't any provision to add any.

PHP conflates both lists and key-value stores into one array data type. JSON doesn't.

answered May 7, 2013 at 13:23

3 Comments

associative array has named keys
@mohammad PHP has "associative arrays", JSON doesn't! JSON's "associative arrays" are called objects.
Thanks, the issue I was having was the structure of the JSON (see my last edit). The problem was caused due to the named keys in my PHP associative array.
3

This is your object:

$parent=new StdClass();
$parent->ID=101;
$parent->ParentID=0;
$parent->Name='Root One';
$child1=new StdClass();
$child1->ID=1011;
$child1->ParentID=$parent->ID;
$child1->Name='Child One';
$parent->Children[]=$child1;
$child1_1=new StdClass();
$child1_1->ID=10111;
$child1_1->ParentID=$child1->ID;
$child1_1->Name='Child One One';
$child1->Children[]=$child1_1; 

This is your JSON convert function:

echo json_encode($parent,JSON_PRETTY_PRINT);

and this is your object coded into JSON format:

{
 "ID": 101,
 "ParentID": 0,
 "Name": "Root One",
 "Children": [
 {
 "ID": 1011,
 "ParentID": 101,
 "Name": "Child One",
 "Children": [
 {
 "ID": 10111,
 "ParentID": 1011,
 "Name": "Child One One"
 }
 ]
 }
 ]
}

The answer came later because I started learning PHP later. Anyway, some day, someone might find it useful.

answered Dec 20, 2014 at 16:58

Comments

2

I have now got a working solution which is fast and works well.

  1. Firstly, as written in SO link from the question;

    In JSON, arrays only have numeric keys, whereas objects have string properties. The inclusion of a array key forces the entire outer structure to be an object by necessity.

    In JSON; Curly braces hold objects ({}), Square brackets hold arrays ([]).

  2. So using a string as a key will result in the json_encode function returning objects, whereas reseting the keys will ensure it creates arrays.

  3. Therefore, just before I return my JSON encoded string, I run a function to reset all the array keys. The code I found on this SO thread (Reset array keys in multidimensional array) was particularly useful!

answered May 7, 2013 at 14:28

Comments

-2

http://php.net/manual/en/function.json-decode.php

Set 2nd parameter of json_decode to true to atleast get assoc arrays.

Aside from that: javascript can't handle non-sequential/non-contiguous array indexes, so as soon as the id's are not sequential, json has no other option then to convert it into "string" indexes.

answered May 7, 2013 at 13:01

3 Comments

Thanks for your answer, it will no doubt be of use in the future, but I was using the returned JSON in jQuery!
That hardly changes the fact that javascript can't handle non sequential array keys. Hence neither can JSON. And hence neither can jquery.
A downvote without comment 1 year after this post.. one wonders why o.O

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.