|
@@ -654,21 +654,26 @@ class TextbookApiService
|
|
|
|
|
|
|
|
private function buildCatalogTree(array $nodes): array
|
|
private function buildCatalogTree(array $nodes): array
|
|
|
{
|
|
{
|
|
|
|
|
+ // 建立ID索引,包含children数组
|
|
|
$indexed = [];
|
|
$indexed = [];
|
|
|
- foreach ($nodes as $node) {
|
|
|
|
|
|
|
+ foreach ($nodes as &$node) { // 使用引用
|
|
|
$node['children'] = [];
|
|
$node['children'] = [];
|
|
|
- $indexed[$node['id']] = $node;
|
|
|
|
|
|
|
+ $indexed[$node['id']] = &$node;
|
|
|
}
|
|
}
|
|
|
|
|
+ unset($node); // 释放引用
|
|
|
|
|
|
|
|
$tree = [];
|
|
$tree = [];
|
|
|
- foreach ($indexed as $id => $node) {
|
|
|
|
|
|
|
+ foreach ($nodes as &$node) { // 使用引用
|
|
|
$parentId = $node['parent_id'] ?? null;
|
|
$parentId = $node['parent_id'] ?? null;
|
|
|
if ($parentId && isset($indexed[$parentId])) {
|
|
if ($parentId && isset($indexed[$parentId])) {
|
|
|
- $indexed[$parentId]['children'][] = $node;
|
|
|
|
|
|
|
+ // 父节点存在,将当前节点添加到父节点的children中
|
|
|
|
|
+ $indexed[$parentId]['children'][] = &$node;
|
|
|
} else {
|
|
} else {
|
|
|
- $tree[] = $node;
|
|
|
|
|
|
|
+ // 根节点,直接添加到树中
|
|
|
|
|
+ $tree[] = &$node;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ unset($node); // 释放引用
|
|
|
|
|
|
|
|
return $tree;
|
|
return $tree;
|
|
|
}
|
|
}
|