$arr = array(
array(
'id' => 1,
'parentid' => 0,
'title' => 'a'
),
array(
'id' => 2,
'parentid' => 1,
'title' => 'a-1'
),
array(
'id' => 3,
'親 ID' => 1,
'タイトル' => 'a-1'
),
array(
'id' => 4,
'parentid' => 2,
'title' => 'a-1-1
)
);
递归转换は数组如下:
$arr = array(
'id' => 1,
'parentid' => 0,
'タイトル' => 'a',
'子' => array(
'id' => 2、 'parentid' => 'title' => 'a-1' => array .....
)Array(
'id' => 3、
'dartid' => 2、
'title' => 'a-2'
)、
.... .... ..
);
请问要 どのように写しますか?
function findChildren($list, $p=0){ $r = array(); foreach($list as $id=>$item){ if($item['parentid'] == $p) { $r[$id] = $item; if($t = findChildren($list, $item['id']) ) $r[$id]['children'][] = $t; } } return $r;} $arr = array( array( 'id' => 1, 'parentid' => 0, 'title' => 'a' ), array( 'id' => 2, 'parentid' => 1, 'title' => 'a-1' ), array( 'id' => 3, 'parentid' => 1, 'title' => 'a-1' ), array( 'id' => 4, 'parentid' => 2, 'title' => 'a-1-1' ) );print_r(findChildren($arr));