Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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": [
share|improve this question
    
It's actually because the indices are not contiguous, i.e. there are gaps. –  Jack May 7 '13 at 13:04
    
Also, your "children" ARE objects (from my perspective) and not arrays. Be glad, and let OOP flow within you. –  STT LCU May 7 '13 at 13:20

3 Answers 3

up vote 0 down vote accepted

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!

share|improve this answer

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.

share|improve this answer
    
associative array has named keys –  mohammad mohsenipur May 7 '13 at 13:25
1  
@mohammad PHP has "associative arrays", JSON doesn't! JSON's "associative arrays" are called objects. –  deceze May 7 '13 at 13:26
    
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. –  Patrick May 7 '13 at 14:23

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.

share|improve this answer
    
Thanks for your answer, it will no doubt be of use in the future, but I was using the returned JSON in jQuery! –  Patrick May 7 '13 at 14:24
    
That hardly changes the fact that javascript can't handle non sequential array keys. Hence neither can JSON. And hence neither can jquery. –  Damien Overeem May 7 '13 at 14:51
    
A downvote without comment 1 year after this post.. one wonders why o.O –  Damien Overeem May 20 at 8:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.