Skip to content

Commit 184cda4

Browse files
committed
fix: corrected possible memory issues in category order, closes #3420
1 parent 32065cb commit 184cda4

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

phpmyfaq/src/phpMyFAQ/Category/Order.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)