0
構建陣列我有一個數組,看起來像這樣:遞歸函數從樹
Array (
[0] => Array
(
[term_id] => 23
[name] => testasdf
[depth] => 1
)
[1] => Array
(
[term_id] => 26
[name] => asdf
[depth] => 2
)
[2] => Array
(
[term_id] => 31
[name] => Another level deep
[depth] => 3
)
[3] => Array
(
[term_id] => 32
[name] => Another level deep
[depth] => 2
)
[4] => Array
(
[term_id] => 24
[name] => testasdf
[depth] => 1
)
[5] => Array
(
[term_id] => 27
[name] => asdf
[depth] => 1
)
)
下面是我使用遞歸函數,它的作品除了在某些情況下(其中深度大於它似乎
function process(&$arr, &$prev_sub = null, $cur_depth = 1) {
$cur_sub = array();
while($line = current($arr)){
if($line['depth'] < $cur_depth){
return $cur_sub;
}elseif($line['depth'] > $cur_depth){
$prev_sub = $this->process($arr, $cur_sub, $cur_depth + 1);
}else{
$cur_sub[$line['term_id']] = array('term_id' => $line['term_id'], 'name' => $line['name']);
$prev_sub =& $cur_sub[$line['term_id']];
next($arr);
}
}
return $cur_sub;
}
這是結果怎麼樣看:
Array
(
[23] => Array
(
[26] => Array
(
[31] => Array
(
[term_id] => 31
[name] => Another level deep
)
)
[32] => Array
(
[term_id] => 32
[name] => Another level deep
)
)
[24] => Array
(
[term_id] => 24
[name] => testasdf
)
[27] => Array
(
[term_id] => 27
[name] => asdf
)
)
任何想法如何,我可以擁有它,因此TE顯示所有深度的rm_id和名稱?
這實際上匹配了正確的ID,我認爲以錯誤的順序返回的term_id的面前,但它仍然沒有顯示term_id和名稱值前兩個節點,似乎任何有子節點的節點都沒有添加term_id和name數組 – dzm