Search code examples
phparraysjsonparsinginfovis

PHP array to Json tree format


I want to tranform an php array into an json string to use with JavaScript InfoVis Toolkit formart.

the objective: InfoVis Demo Tree

Json specification format: InfoVis-loading and serving JSON data

I have this php array: $my_array :

Array 
( 
    [item_1] => Array 
        ( 
            [id] => item_1_ID 
            [name] => item_1_NAME 
            [data] => item_1_DATA 
            [children] => Array 
                ( 
                    [door] => Array 
                        ( 
                            [id] => door_ID 
                            [name] => door_NAME 
                            [data] => door_DATA 
                            [children] => Array 
                            ( 
                                [mozart] => Array 
                                    ( 
                                        [id] => mozart_ID 
                                        [name] => mozart_NAME 
                                        [data] => mozart_DATA 
                                        [children] => Array 
                                            ( 
                                                [grass] => Array 
                                                    ( 
                                                        [id] => grass_ID 
                                                        [name] => grass_NAME 
                                                        [data] => yes 
                                                    ) 

                                                [green] => Array 
                                                    ( 
                                                        [id] => green_ID 
                                                        [name] => green_NAME 
                                                        [data] => no 
                                                    ) 

                                                [human] => Array 
                                                    ( 
                                                        [id] => human_ID 
                                                        [name] => human_NAME 
                                                        [data] => human_DATA 
                                                        [children] => Array 
                                                            ( 
                                                                [blue] => Array 
                                                                    ( 
                                                                        [id] => blue_ID 
                                                                        [name] => blue_NAME 
                                                                        [data] => blue_DATA 
                                                                        [children] => Array 
                                                                            ( 
                                                                                [movie] => Array 
                                                                                    ( 
                                                                                        [id] => movie_ID 
                                                                                        [name] => movie_NAME 
                                                                                        [data] => yes 
                                                                                    ) 

                                                                            ) 

                                                                    ) 

                                                            ) 

                                                    ) 

                                            ) 

                                    ) 

                            ) 

                    ) 

                [beat] => Array 
                    ( 
                        [id] => beat_ID 
                        [name] => beat_NAME 
                        [data] => yes 
                    ) 

                [music] => Array 
                    ( 
                        [id] => music_ID 
                        [name] => music_NAME 
                        [data] => no 
                    ) 

            ) 

    ) 

)           

now if I json_encode($my_array);

{ 
    "item_1": { 
        "id": "item_1_ID", 
        "name": "item_1_NAME", 
        "data": "item_1_DATA", 
        "children": { 
            "door": { 
                "id": "door_ID", 
                "name": "door_NAME", 
                "data": "door_DATA", 
                "children": { 
                    "mozart": { 
                        "id": "mozart_ID", 
                        "name": "mozart_NAME", 
                        "data": "mozart_DATA", 
                        "children": { 
                            "grass": { 
                                "id": "grass_ID", 
                                "name": "grass_NAME", 
                                "data": "yes" 
                            }, 
                            "green": { 
                                "id": "green_ID", 
                                "name": "green_NAME", 
                                "data": "no" 
                            }, 
                            "human": { 
                                "id": "human_ID", 
                                "name": "human_NAME", 
                                "data": "human_DATA", 
                                "children": { 
                                    "blue": { 
                                        "id": "blue_ID", 
                                        "name": "blue_NAME", 
                                        "data": "blue_DATA", 
                                        "children": { 
                                            "movie": { 
                                                "id": "movie_ID", 
                                                "name": "movie_NAME", 
                                                "data": "yes" 
                                            } 
                                        } 
                                    } 
                                } 
                            } 
                        } 
                    } 
                } 
            }, 
            "beat": { 
                "id": "beat_ID", 
                "name": "beat_NAME", 
                "data": "yes" 
            }, 
            "music": { 
                "id": "music_ID", 
                "name": "music_NAME", 
                "data": "no" 
            } 
        } 
    } 
}  

But to InfoVis the current json output (json_encode($my_array)) has 3 problems:

  1. is not using [ ]
  2. the 'children' arrays have the key names
  3. arrays items is with their key names -> example: "item_1": { ....

let me point the problem so maybe you can help with an function to transform this json string:

see this slice of json_encode($my_array) output:

{  
    "item_1": {  
        "id": "item_1_ID",  
        "name": "item_1_NAME",  
        "data": "item_1_DATA",  
        "children": {  
            "door": {  
                "id": "door_ID",  

1. problem 1:

{  
    "item_1": {  

we have to remove those keys like: "item_1":

2. problem 2:

"children": {  
            "door": {  
                "id": "door_ID",  

the correct code for this should be:

"children": [  
            {  
                "id": "door_ID",......  

"door": was removed... because it`s a key

"children": { => becomes" "children": [

An working example of 'children':

"children": [  
    {  
        "id": "grass_ID",  
        "name": "grass_NAME",  
        "data": "yes"  
    },  
    {  
        "id": "green_ID",  
        "name": "green_NAME",  
        "data": "no"  
    }  
]  

to clarify an complete example of WORKING Json InfoVis format:

json = {
        id: "node02",
        name: "0.2",
        children: [{
            id: "node13",
            name: "1.3",
            children: [{
                id: "node24",
                name: "2.4"
              }, {
                id: "node222",
                name: "2.22"
              }]
        }, {
            id: "node125",
            name: "1.25",
            children: [{
                id: "node226",
                name: "2.26"
            }, {
                id: "node237",
                name: "2.37"
            }, {
                id: "node258",
                name: "2.58"
            }]
        }, {
            id: "node165",
            name: "1.65",
            children: [{
                id: "node266",
                name: "2.66"
            }, {
                id: "node283",
                name: "2.83"
            }, {
                id: "node2104",
                name: "2.104"
            }, {
                id: "node2109",
                name: "2.109"
            }, {
                id: "node2125",
                name: "2.125"
            }]
        }, {
            id: "node1130",
            name: "1.130",
            children: [{
                id: "node2131",
                name: "2.131"
            }, {
                id: "node2138",
                name: "2.138"
            }]
        }]
    };

is it clear to understand?

Hope anyone can help me.. I'm working on this for days!

thank you.


Solution

  • Try this quickie conversion function

    function fixit($yourArray) {
        $myArray = array();
        foreach ($yourArray as $itemKey => $itemObj) {
            $item = array();
            foreach ($itemObj as $key => $value) {
                if (strtolower($key) == 'children') {
                    $item[$key] = fixit($value);
                } else {
                    $item[$key] = $value;
                }
            }
            $myArray[] = $item;
        }
        return $myArray;
    }
    
    $fixed = fixit($my_array);
    $json = json_encode($fixed);