<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                > ``` > public function treeBack($data,$pid){ > foreach($data as $k=>$v){ > if($v['pid'] == $pid){ > $v['child'] = $this->treeBack($data,$v['id']); > $tree[] = $v; > unset($data[$k]); > } > } > return $tree; > } > ``` - 如上代碼,這是一段很經典的無限遞歸循環邏輯,但是當數據量超過2000 - 3000行時,效率大打折扣,經過我們的實際應用測試,在沒有優化這段代碼之前,遞歸所耗費的時間是 2s 左右 - 但是經過優化之后,遞歸邏輯耗費的時間僅僅只有 86ms 左右 - OK,貼出下優化后的代碼再來分析 > ``` > public function treeBack($data, $pidKey='pid', $idKey = 'id', $childKey='child') { > $grouped = array(); > foreach ($flat as $sub){ > $grouped[$sub[$pidKey]][] = $sub; > } > $fnBuilder = function($siblings) use (&$fnBuilder, $grouped, $idKey, $childKey) { > foreach ($siblings as $k => $sibling) { > $id = $sibling[$idKey]; > if(isset($grouped[$id])) { > $sibling[$childKey] = $fnBuilder($grouped[$id]); > usort($sibling[$childKey], function($a, $b) use ($childKey) { > if ($a[$childKey] == $b[$childKey]) { > return 0; > } > return ($a['weight'] < $b['weight']) ? -1 : 1; > }); > } > $siblings[$k] = $sibling; > } > return $siblings; > }; > $tree = $fnBuilder($grouped[0]); > return $tree; > } > ``` - 恩,你沒有看錯,將數據重新構造,然后進行匿名函數循環
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看