ホームページ >バックエンド開発 >PHPチュートリアル >なぜ再帰は機能しないのでしょうか?
php での再帰的トラバーサル
Array( [0] => Array ( [0] => 1 [catid] => 1 [1] => 0 [parentid] => 0 [2] => 关于我们 [catname] => 关于我们 [3] => 4 [listorder] => 4 [4] => 0 [items] => 0 [5] => 1 [type] => 1 [child] => Array ( [0] => Array ( [0] => 2 [catid] => 2 [1] => 1 [parentid] => 1 [2] => 关于我们 [catname] => 关于我们 [3] => 1 [listorder] => 1 [4] => 0 [items] => 0 [5] => 1 [type] => 1 ) [1] => Array ( [0] => 3 [catid] => 3 [1] => 1 [parentid] => 1 [2] => 公司荣誉 [catname] => 公司荣誉 [3] => 2 [listorder] => 2 [4] => 0 [items] => 0 [5] => 1 [type] => 1 ) [2] => Array ( [0] => 4 [catid] => 4 [1] => 1 [parentid] => 1 [2] => 联系我们 [catname] => 联系我们 [3] => 3 [listorder] => 3 [4] => 0 [items] => 0 [5] => 1 [type] => 1 ) [3] => Array ( [0] => 5 [catid] => 5 [1] => 1 [parentid] => 1 [2] => 加入我们 [catname] => 加入我们 [3] => 4 [listorder] => 4 [4] => 0 [items] => 0 [5] => 1 [type] => 1 ) [4] => Array ( [0] => 6 [catid] => 6 [1] => 1 [parentid] => 1 [2] => 友情链接 [catname] => 友情链接 [3] => 5 [listorder] => 5 [4] => 0 [items] => 0 [5] => 2 [type] => 2 ) ) ))
function getTree($arr){// $row = $arr; foreach($arr as $k=>$v) { $tree .= '<li><span><a href="content.php?catid='.$v['catid'].'" target="right">'.$v['catname']."</a></span>"; if(is_array($v['child'])){ $tree .= '<ul>'; //遍历child子数组 foreach($v['child'] as $sv){ $tree .= '<li><span><a href="content.php?catid='.$sv['catid'].'" target="right">'.$sv['catname']."</a></span>"; // getTree($v['child']); if(is_array($sv['child'])) { $tree .= '<ul>'; foreach($sv['child'] as $ssv){ $tree .= '<li><a href="content.php?catid='.$ssv['catid'].'" target="right">'.$ssv['catname']."</a></li>"; } $tree .= "</ul>"; } $tree .= "</li>"; }// print_r($v['child']);// getTree($v['child']); $tree .= "</ul>"; } $tree .= '</li>'; } return $tree;}
あなたは再帰的ではありません! getTree($v['child']); はすべてコメントアウトされています
getTree には return $tree; があります
再帰的な場合は $tree .= getTree($v['child']); と書く必要があります
再発していません! getTree($v['child']); はすべてコメントアウトされています
getTree には return $tree; があります
再帰的な場合は $tree .= getTree($v['child']); と書く必要があります
ありがとうございます、コメントアウトしてください。出力はなく、それについてできることは何もありません
$tree .= getTree($v['child']) と書きます。ため息
私は、ありがとうと言いました