Skip to content

Commit 58e928a

Browse files
committed
Merge branch '4.0' into 'main'
2 parents e6e426e + 849bf45 commit 58e928a

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

phpmyfaq/src/phpMyFAQ/Category/Order.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,18 @@ public function setCategoryTree(
121121
public function getCategoryTree(array $categories, int $parentId = 0): array
122122
{
123123
$result = [];
124+
$stack = [[$parentId, &$result]];
124125

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+
}
129136
}
130137
}
131138

0 commit comments

Comments
 (0)