我有这种格式的数组:
array( array('id' => 1, 'parent_id' => null, 'name' => 'lorem ipsum'), array('id' => 2, 'parent_id' => 1, 'name' => 'lorem ipsum1'), array('id' => 3, 'parent_id' => 1, 'name' => 'lorem ipsum2'), array('id' => 4, 'parent_id' => 2, 'name' => 'lorem ipsum3'), array('id' => 5, 'parent_id' => 3, 'name' => 'lorem ipsum4'), array('id' => 6, 'parent_id' => null, 'name' => 'lorem ipsum5'), );
我必须使用以下样式将此数组转换为json对象:
var json = { id: "1", name: "loreim ipsum", data: {}, children: [{ id: "2", name: "lorem ipsum1", data: {}, children: [{ id: "3", name: "lorem ipsum2", data: {}, children: [{ ..............
我怎样才能做到这一点?谢谢。
我的解决方案:
$data = array( array('id' => 1, 'parent_id' => null, 'name' => 'lorem ipsum'), array('id' => 2, 'parent_id' => 1, 'name' => 'lorem ipsum1'), array('id' => 3, 'parent_id' => 1, 'name' => 'lorem ipsum2'), array('id' => 4, 'parent_id' => 2, 'name' => 'lorem ipsum3'), array('id' => 5, 'parent_id' => 3, 'name' => 'lorem ipsum4'), array('id' => 6, 'parent_id' => null, 'name' => 'lorem ipsum5'), ); $itemsByReference = array(); // Build array of item references: foreach($data as $key => &$item) { $itemsByReference[$item['id']] = &$item; // Children array: $itemsByReference[$item['id']]['children'] = array(); // Empty data class (so that json_encode adds "data: {}" ) $itemsByReference[$item['id']]['data'] = new StdClass(); } // Set items as children of the relevant parent item. foreach($data as $key => &$item) if($item['parent_id'] && isset($itemsByReference[$item['parent_id']])) $itemsByReference [$item['parent_id']]['children'][] = &$item; // Remove items that were added to parents elsewhere: foreach($data as $key => &$item) { if($item['parent_id'] && isset($itemsByReference[$item['parent_id']])) unset($data[$key]); } // Encode: $json = json_encode($data);