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": [