일반적인 중첩 트리 모델을 가지고 있는데 레벨이나 깊이를 기반으로 '자식'배열로 배열을 만들고 싶지만 저에게는 효과가없는 것 같습니다. . 여기에 내가 지금 가지고있는 것 :PHP 배열에서 중첩 된 트리 모델을 통과하지 못하도록
while($this->tax->getTreeNext($nodes))
{
$level = $this->tax->getTreeLevel($nodes);
if($level != 0){
echo $level . '-' . $current_level;
if($level > $current_level){
$terms[$i] = array(
'term_id' => $terms[$i-1]['term_id'],
'name' => $terms[$i-1]['name'],
'level' => $terms[$i-1]['level'],
'children' => array(
'term_id' => $nodes['row']['term_id'],
'name' => $nodes['row']['name'],
'level' => $level,
)
);
unset($terms[$i-1]);
}else{
$terms[$i] = array(
'term_id' => $nodes['row']['term_id'],
'name' => $nodes['row']['name'],
'level' => $level
);
}
$current_level = $level;
$i++;
}
}
이것은 하나의 자식에게는 효과가 있지만 아이들에게는 자식이 없을 때 ...이 문제를 해결하는 방법은 무엇입니까?
감사합니다.
편집 :
이 작업에 가까운 것으로 나타납니다 최신입니다 : 나무는 각 노드에 관련된 깊이 값이에 전달
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;
}
. 현재 문제는 elseif($line['depth'] > $cur_depth)
입니다. 노드에 자식이있는 경우 자식에 대한 배열 만 반환하지만 해당 노드 이름이나 term_id는 포함되지 않습니다.
감사합니다.
반복 대신 반복. – usoban
잘 모르겠습니다 ... 예를 게시 할 수 있습니까? – dzm
데이터 구조에 대해 좀 더 자세히 설명해 주시겠습니까? 당신의 나무는 어떻게 정의되고 저장됩니까? –