2012-09-10 32 views
-1

我:php json_encode格式化结果?

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

]

为了与选择2 jQuery插件的工作,我正在合作。 '孩子'的名字并不重要,我认为这只是一个占位符,所以它得到正确的解析。我不知道我会怎么做,我一直在尝试str_replace(),但即使这样做还没有那么好。

+1

什么你问的需要自定义您的编码部分,尤其是因为你正在寻找的输出是无效的json。 – bdares

+0

未加引号的键是无效的JSON。你真的想*那*吗?如果你只是在谈论数组结构......在JSON编码之前调整结构以满足需要。 – deceze

+0

钥匙可以或不可以被引用。就像我在网上找到的例子,通过谷歌的格式,他们没有被引用,这对我所需要的,但引用将工作以及。 – Dev

回答

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"}]}] 
+0

OP想要JSON字符串漂亮。生成它不是问题。 –

+0

@MarcB问题很不清楚。你认为这只是格式问题吗?为什么[select2](http://ivaynberg.github.com/select2/)非常关心JSON呢? – Tchoupi

+0

IT花了一点,Mathieu,但我能够得到这个为我工作。谢谢。 – Dev