我得到了一個當前echo結果的遞歸函數。我希望這個函數返回結果并使用foreach內部標記循環它們。我知道我應該對每個數組使用某種迭代來獲得我想要的結果,但失敗了。目前我的代碼看起來像這樣(嘗試迭代):public static function recursiveChildCategory($categories = array(), $depth = 0, $i = 0) { $ca = []; // Loop through categories foreach($categories as $key => $category){ echo str_repeat(" ", $depth); echo "<a href='".implode('/', $category['breadcrumb'])."'>{$category['title']}</a>"; echo '<br>'; $ca[$i] = [ 'id' => $category['id'], 'title' => $category['title'], ]; if(isset($category['child'])) { // Loop self::recursiveChildCategory($category['child'], $depth + 1, $i++); } } return $ca;}和傳入數組到函數:Array ( [0] => Array ( [id] => 7 [title] => Deserts [slug] => deserts [child] => Array ( [0] => Array ( [id] => 8 [title] => Space [slug] => space [child] => Array ( [0] => Array ( [id] => [title] => [slug] => [child] => Array ( ) ) ) ) ) ) )目前它只返回第一級子類別“沙漠”,但不返回“空間”。作為期望的結果,我希望函數返回所有類別$depth和多個子類別的無限路徑(與當前echo做的工作相同)。
- 1 回答
- 0 關注
- 178 瀏覽
添加回答
舉報
0/150
提交
取消