Jelajahi Sumber

教材目录API内容完整呈现

yemeishu 1 hari lalu
induk
melakukan
319aba5da2
1 mengubah file dengan 10 tambahan dan 5 penghapusan
  1. 10 5
      app/Services/TextbookApiService.php

+ 10 - 5
app/Services/TextbookApiService.php

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