php json_encode格式化结果?

时间:2012-09-10 14:46:02

标签: php json jquery-plugins

我有:

$all = array(
    array('id'=>1, 'cat'=>'Main','type'=>'Name0'),
    array('id'=>2, 'cat'=>'Main','type'=>'Name1'),
    array('id'=>3, 'cat'=>'Main','type'=>'Name3'),
    array('id'=>4, 'cat'=>'Main','type'=>'Name4'),
    array('id'=>5, 'cat'=>'Secondary','type'=>'Name5'),
    array('id'=>6, 'cat'=>'Secondary','type'=>'Name6'),
    array('id'=>7, 'cat'=>'Secondary','type'=>'Name7'),
    array('id'=>8, 'cat'=>'Other','type'=>'Name8'),
    array('id'=>9, 'cat'=>'Other','type'=>'Name9'),
    array('id'=>10, 'cat'=>'Other','type'=>'Name10'),
    array('id'=>11, 'cat'=>'Other','type'=>'Name11'),
);

$result = array();
    foreach($all as $array){
    $result[$array['cat']][] = array('id'=>$array['id'],'type'=>$array['type']);
}

$json_type = json_encode($result);

返回:

{"Main":[{"id":"1","type":"name1"},{"id":"2","type":"name2"},{"id":"3","type":"name3"},{"id":"4","type":"name4"}],"Secondary":[{"id":"5","type":"name5"},{"id":"6","type":"name6"},{"id":"7","type":"name7"}],"Other":[{"id":"8","type":"name8"},{"id":"9","type":"name9"},{"id":"10","type":"name10"},{"id":"11","type":"name11"}]}

但我需要它返回:

[
{
    "text": "Main",
    "children": [
        {
            "id": "1",
            "text": "name1"
        },
        {
            "id": "2",
            "text": "name2"
        },
        {
            "id": "3",
            "text": "name3"
        },
        {
            "id": "4",
            "text": "name4"
        }
    ]
},
{
    "text": "Secondary",
    "children": [
        {
            "id": "5",
            "text": "name5"
        },
        {
            "id": "6",
            "text": "name6"
        },
        {
            "id": "7",
            "text": "name7"
        }
    ]
},
{
    "text": "Other",
    "children": [
        {
            "id": "8",
            "text": "name8"
        },
        {
            "id": "9",
            "text": "name9"
        },
        {
            "id": "10",
            "text": "name10"
        },
        {
            "id": "11",
            "text": "name11"
        }
    ]
}

要使用select2 JQuery插件,我正在使用。 '孩子'的名字并不重要,我认为这只是一个占位符,所以它被正确解析。我不确定我会怎么做,我一直在尝试str_replace(),但即使这样做也没那么好。

1 个答案:

答案 0 :(得分:1)

我想在2个循环中。第一个按类别对结果进行分组,第二个按照您的需要对其进行格式化:

$temp = array();
foreach($all as $array){    
    if (!isset($temp[$array['cat']])) {
        $temp[$array['cat']] = array();
    }

    $temp[$array['cat']][] = array('id'=>$array['id'], 'type'=>$array['type']);
}

$result = array();
foreach ($temp as $key=>$value) {
    $result[] = array('text'=>$key, 'children'=>$value);
}

echo json_encode($result);

这会产生以下输出:

[{"text":"Main","children":[{"id":1,"type":"Name0"},{"id":2,"type":"Name1"},{"id":3,"type":"Name3"},{"id":4,"type":"Name4"}]},{"text":"Secondary","children":[{"id":5,"type":"Name5"},{"id":6,"type":"Name6"},{"id":7,"type":"Name7"}]},{"text":"Other","children":[{"id":8,"type":"Name8"},{"id":9,"type":"Name9"},{"id":10,"type":"Name10"},{"id":11,"type":"Name11"}]}]