PHP数组为Json树格式

时间:2012-01-04 16:49:40

标签: php arrays json parsing infovis

我想将 php数组转换为 json字符串,以便与JavaScript InfoVis Toolkit formart 一起使用。

目标: InfoVis Demo Tree

Json规范格式: InfoVis-loading and serving JSON data

我有这个php数组:$ 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 
                    ) 

            ) 

    ) 

)           

现在如果我是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" 
            } 
        } 
    } 
}  

但是对InfoVis来说,当前的json输出(json_encode($ my_array))有3个问题:

  1. 未使用[]
  2. 'children'数组具有键名
  3. 数组项目的键名称为>例如:“item_1”:{....
  4. 让我指出问题所以也许你可以帮助一个函数来转换这个json字符串:

    看到这片json_encode($ my_array)输出:

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

    1。问题1:

    {  
        "item_1": {  
    
      

    我们必须删除这些键,例如:“item_1”:

    2。问题2:

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

    正确的代码应该是:

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

    “门”:被删除了...因为它是一把钥匙

         

    “孩子们”:{=>成为“”孩子“:[

    '孩子'的工作示例:

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

    以阐明WORKING Json InfoVis格式的完整示例:

    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"
                }]
            }]
        };
    

    理解是否清楚?

    希望任何人都可以帮助我..我正在研究这几天!

    谢谢。

3 个答案:

答案 0 :(得分:4)

试试这个快速转换功能

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);

答案 1 :(得分:2)

这是记录在案的行为。当JSON使用json_encode进行字符串化时,关联数组将生成对象文字。更新原始数组结构以表示所需的结果,而不是修改生成的JSON表示,或者为json_encode包装每个对象的自己的解决方案。

编辑:尝试清理操作

代码

$original = <your original data-array>; // assumed, I reversed your encoded JSON as test data

// Start by stripping out the associative keys for level 1
$clean = array_values($original);

// Then recursively descend array, and do the same for every children-property encountered
function &recursiveChildKeysCleaner(&$arr) {
    // If $arr contains 'children'...
    if (array_key_exists('children', $arr)) {
        /// ...strip out associative keys
        $arr['children'] = array_values($arr['children']);

        // ...and descend each child
        foreach ($arr['children'] as &$child) {
            recursiveChildKeysCleaner($child);
        }
    }

    return $arr;
}

foreach ($clean as &$item) {
    recursiveChildKeysCleaner($item);
}
unset($item);
echo json_encode($clean);

输出

[{
    "id": "item_1_ID",
    "name": "item_1_NAME",
    "data": "item_1_DATA",
    "children": [{
        "id": "door_ID",
        "name": "door_NAME",
        "data": "door_DATA",
        "children": [{
            "id": "mozart_ID",
            "name": "mozart_NAME",
            "data": "mozart_DATA",
            "children": [{
                "id": "grass_ID",
                "name": "grass_NAME",
                "data": "yes"
            },
            {
                "id": "green_ID",
                "name": "green_NAME",
                "data": "no"
            },
            {
                "id": "human_ID",
                "name": "human_NAME",
                "data": "human_DATA",
                "children": [{
                    "id": "blue_ID",
                    "name": "blue_NAME",
                    "data": "blue_DATA",
                    "children": [{
                        "id": "movie_ID",
                        "name": "movie_NAME",
                        "data": "yes"
                    }]
                }]
            }]
        }]
    },
    {
        "id": "beat_ID",
        "name": "beat_NAME",
        "data": "yes"
    },
    {
        "id": "music_ID",
        "name": "music_NAME",
        "data": "no"
    }]
}]

答案 2 :(得分:2)

PHP不区分数组(数字键)和关联数组(字符串键)。他们都只是阵列。 Javascript有区别。由于您使用的是字符串键,因此必须将它们作为JS中的对象({})来完成。

你不能告诉json_encode忽略数组中的键(例如你的'children'子数组)。这意味着生成的JSON NOT 与原始PHP结构相同 - 您现在已经更改了键名。

您必须处理数组并将所有子子数组键转换为数字:

grass -> 0
green -> 1
etc...

这样json-encode就可以看到它是一个数字键控的PHP数组,这意味着它将生成一个实际的javavscript数组([]),而不是一个对象({})。

另一种方法是编写自己的JSON编码器,为您即时执行此操作。