File tree 1 file changed +11
-4
lines changed
phpmyfaq/src/phpMyFAQ/Category
1 file changed +11
-4
lines changed Original file line number Diff line number Diff line change @@ -121,11 +121,18 @@ public function setCategoryTree(
121
121
public function getCategoryTree (array $ categories , int $ parentId = 0 ): array
122
122
{
123
123
$ result = [];
124
+ $ stack = [[$ parentId , &$ result ]];
124
125
125
- foreach ($ categories as $ category ) {
126
- if ((int )$ category ['parent_id ' ] === $ parentId ) {
127
- $ childCategories = $ this ->getCategoryTree ($ categories , (int )$ category ['category_id ' ]);
128
- $ result [$ category ['category_id ' ]] = $ childCategories ;
126
+ while (!empty ($ stack )) {
127
+ $ popped = array_pop ($ stack );
128
+ $ currentParentId = $ popped [0 ];
129
+ $ currentResult = &$ popped [1 ];
130
+
131
+ foreach ($ categories as $ category ) {
132
+ if ((int )$ category ['parent_id ' ] === $ parentId ) {
133
+ $ childCategories = $ this ->getCategoryTree ($ categories , (int )$ category ['category_id ' ]);
134
+ $ result [$ category ['category_id ' ]] = $ childCategories ;
135
+ }
129
136
}
130
137
}
131
138
You can’t perform that action at this time.
0 commit comments