File tree Expand file tree Collapse file tree
phpmyfaq/src/phpMyFAQ/Category Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -124,11 +124,16 @@ public function setCategoryTree(
124124 public function getCategoryTree (array $ categories , int $ parentId = 0 ): array
125125 {
126126 $ result = [];
127+ $ stack = [[$ parentId , &$ result ]];
127128
128- foreach ($ categories as $ category ) {
129- if ((int ) $ category ['parent_id ' ] === $ parentId ) {
130- $ children = $ this ->getCategoryTree ($ categories , $ category ['category_id ' ]);
131- $ result [$ category ['category_id ' ]] = $ children ;
129+ while (!empty ($ stack )) {
130+ list ($ currentParentId , &$ currentResult ) = array_pop ($ stack );
131+
132+ foreach ($ categories as $ category ) {
133+ if ((int ) $ category ['parent_id ' ] === $ currentParentId ) {
134+ $ currentResult [$ category ['category_id ' ]] = [];
135+ $ stack [] = [$ category ['category_id ' ], &$ currentResult [$ category ['category_id ' ]]];
136+ }
132137 }
133138 }
134139
You can’t perform that action at this time.
0 commit comments