Json_encode () in a multidimensional array - with string keys

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

Critically, I call the identifiers of each object - this helps me put every object under the correct parent. (In the code below I use 101, 102, etc.)

However, the problem is that I am returning the array in JSON using json_encode . Each Kids array is printed as an object, not an array - as shown in the JSON code below.

As I read in another SO thread here , they are "created as objects due to 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 problem?

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

 [ { "ID": "101", "ParentID": "0", "Name": "Root One", "Children": [ { "ID": "102", "ParentID": "101", "Name": "Child One", "Children": [ 
+4
source share
4 answers

I now have a working solution that works quickly and well.

  • First, as written in the SO link from the question;

    In JSON, arrays have only numeric keys, while objects have a property string. The inclusion of an array key causes the entire external structure to be an object as needed.

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

  • Thus, using the string as the key will return json_encode objects, while restarting the keys will create arrays.

  • Therefore, before I return my encoded JSON string, I ran the reset function of all the keys in the array. The code I found in this SO thread (Reset array keys in a multidimensional array) was especially useful!

+1
source

There are no explicit indexes in the JSON array, this is just an ordered list. The only JSON data structure that has named keys is an object. The literal should make this very obvious:

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

This array has no named indexes, and there are no conditions for adding any.

PHP combines both lists and keystores into one array data type. JSON does not.

+4
source

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

Set the 2nd parameter json_decode to true to access the arrays.

In addition: javascript cannot process indexes without a sequential / non-contiguous array, therefore, as soon as the identifier is not sequential, json has no other option to convert it to "string" indexes.

0
source

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 conversion function:

 echo json_encode($parent,JSON_PRETTY_PRINT); 

and this is your object encoded in 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, someday someone might find this helpful.

0
source

Source: https://habr.com/ru/post/1479531/


All Articles