关于将JSON树结构化为PHP

时间:2013-08-25 10:42:46

标签: php json tree structure

最近我将JSON类别结构制作为PHP。 但是,我不是php程序员。所以,我做不好。

下面有我的PHP源代码。

<?php
header('Content-Type: application/json; charset=utf-8');

$data = array(
    array('categoryno' => 1, 'parentno' => null, 'name' => urlencode('book')),
    array('categoryno' => 2, 'parentno' => 1, 'name' => urlencode('science')),
    array('categoryno' => 3, 'parentno' => 1, 'name' => urlencode('medical')),
    array('categoryno' => 4, 'parentno' => 2, 'name' => urlencode('physics')),
    array('categoryno' => 5, 'parentno' => 3, 'name' => urlencode('medicine')),
    array('categoryno' => 6, 'parentno' => null, 'name' => urlencode('game')),
    array('categoryno' => 7, 'parentno' => 6, 'name' => urlencode('fantasy')),
);


$itemArray = array();

foreach($data as $key => &$item)
{
    $itemArray[$item['categoryno']] = &$item;

    $itemArray[$item['categoryno']]['children'] = array();

    $itemArray[$item['categoryno']] = Array('item' => $itemArray[$item['categoryno']]);


    if($item['item']['parentno'] && isset($itemArray[$item['item']['parentno']]))
    {
        $itemArray[$item['item']['parentno']]['children'][] = &$item;
    }

    if($item['item']['parentno'] && isset($itemArray[$item['item']['parentno']]))
    {
        unset($data[$key]);
    }         
}

$json = json_encode(Array('category' => $data));
echo urldecode($json);
?>

这是当前的结果。

{
    "category":{
        "0":{
            "item":{
                "categoryno":1,
                "parentno":null,
                "name":"book",
                "children":[
                ]
            },
            "children":[
                {
                    "item":{
                        "categoryno":2,
                        "parentno":1,
                        "name":"science",
                        "children":[
                        ]
                    },
                    "children":[
                        {
                            "item":{
                                "categoryno":4,
                                "parentno":2,
                                "name":"physics",
                                "children":[
                                ]
                            }
                        }
                    ]
                },
                {
                    "item":{
                        "categoryno":3,
                        "parentno":1,
                        "name":"medical",
                        "children":[
                        ]
                    },
                    "children":[
                        {
                            "item":{
                                "categoryno":5,
                                "parentno":3,
                                "name":"medicine",
                                "children":[
                                ]
                            }
                        }
                    ]
                }
            ]
        },
        "5":{
            "item":{
                "categoryno":6,
                "parentno":null,
                "name":"game",
                "children":[
                ]
            },
            "children":[
                {
                    "item":{
                        "categoryno":7,
                        "parentno":6,
                        "name":"fantasy",
                        "children":[
                        ]
                    }
                }
            ]
        }
}

但是,我想把结果如下所示。

{
    "category":{
        "item":{
            "categoryno":1,
            "parentno":null,
            "name":"book",
            "children":[
                "item":{
                    "categoryno":2,
                    "parentno":1,
                    "name":"science",
                    "children":[
                        "item":{
                            "categoryno":4,
                            "parentno":2,
                            "name":"physics",
                            "children":[
                            ]
                        }
                    ]
                },
                "item":{
                    "categoryno":3,
                    "parentno":1,
                    "name":"medical",
                    "children":[
                        "item":{
                            "categoryno":5,
                            "parentno":3,
                            "name":"medicine",
                            "children":[
                            ]
                        }
                    ]
                },
            ]
        },
        "item":{
            "categoryno":6,
            "parentno":null,
            "name":"game",
            "children":[
                "item":{
                    "categoryno":7,
                    "parentno":6,
                    "name":"fantasy",
                    "children":[
                    ]
                }
            ]
        },
    }
}

如何修复我的代码? 你能帮我吗?

1 个答案:

答案 0 :(得分:0)

改变

$itemArray[$item['categoryno']]['children'] = array();

$itemArray[$item['categoryno']] = Array('item' => $itemArray[$item['categoryno']]);
像这样

$itemArray[$item['categoryno']]['children'] = Array('item' => $itemArray[$item['categoryno']]);