{"id":26077,"date":"2024-11-24T11:12:00","date_gmt":"2024-11-24T03:12:00","guid":{"rendered":"https:\/\/fwq.ai\/blog\/26077\/"},"modified":"2024-11-24T11:12:00","modified_gmt":"2024-11-24T03:12:00","slug":"thinkphp%e6%a1%86%e6%9e%b6%e4%b8%ad%e5%a6%82%e4%bd%95%e5%b0%86%e6%97%a0%e9%99%90%e7%ba%a7%e5%88%86%e7%b1%bb%e7%9a%84%e4%b8%80%e7%bb%b4%e6%95%b0%e7%bb%84%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%a4%9a%e7%bb%b4-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/26077\/","title":{"rendered":"ThinkPHP\u6846\u67b6\u4e2d\u5982\u4f55\u5c06\u65e0\u9650\u7ea7\u5206\u7c7b\u7684\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u591a\u7ef4\u6570\u7ec4\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173079100418671.jpg\" class=\"aligncenter\" title=\"ThinkPHP\u6846\u67b6\u4e2d\u5982\u4f55\u5c06\u65e0\u9650\u7ea7\u5206\u7c7b\u7684\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u591a\u7ef4\u6570\u7ec4\uff1f\u63d2\u56fe\" alt=\"ThinkPHP\u6846\u67b6\u4e2d\u5982\u4f55\u5c06\u65e0\u9650\u7ea7\u5206\u7c7b\u7684\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u591a\u7ef4\u6570\u7ec4\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u65e0\u9650\u7ea7\u5206\u7c7b\u5b50\u5206\u7c7b\u8bfb\u53d6<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong><\/p>\n<p>\u5982\u4f55\u4f7f\u7528  \u6846\u67b6\u8bfb\u53d6\u65e0\u9650\u7ea7\u5206\u7c7b\u4e2d\u7684\u6240\u6709\u5b50\u5206\u7c7b\uff0c\u5e76\u4ee5\u591a\u7ef4\u6570\u7ec4\u5f62\u5f0f\u8fd4\u56de\uff1f<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u9996\u5148\uff0c\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u51fd\u6570 getchildarea \u6765\u8bfb\u53d6\u5b50\u5206\u7c7b\uff0c\u8be5\u51fd\u6570\u5c06\u4ee5\u7ed9\u5b9a\u7684\u5730\u533a id \u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u4ee5\u9012\u5f52\u65b9\u5f0f\u904d\u5386\u6240\u6709\u5b50\u5206\u7c7b\u3002<\/p>\n<pre>function getchildarea($id)\n{\n    if (!$id) {\n        return;\n    }\n    static $area;\n    $area = $area ?? new ppcommonmodelrea;\n    $result = collection($area-&gt;where(['pid' =&gt; $id])-&gt;order('id desc')-&gt;select())-&gt;toarray();\n    static $res = [];\n    if ($result) {\n        foreach ($result as $key =&gt; $val) {\n            $res[] = $val;\n            getchildarea($val['id']);\n        }\n    }\n    return $res;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7136\u800c\uff0c\u8fd9\u4e2a\u51fd\u6570\u8fd4\u56de\u7684\u662f\u4e00\u7ef4\u6570\u7ec4\u3002\u8981\u83b7\u5f97\u591a\u7ef4\u6570\u7ec4\uff0c\u6211\u4eec\u9700\u8981\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u591a\u7ef4\u6570\u7ec4\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 deal_list_to_tree2 \u51fd\u6570\u6765\u5b9e\u73b0\u6b64\u76ee\u7684\uff1a<\/p>\n<pre>function deal_list_to_tree2($data, $pkname = 'id', $pidname = 'parent_id', $childname = 'children_list', $is_empty_childrens = false, $rootid = '')\n{\n    $new_data = [];\n    if (!empty($data)) {\n        foreach ($data as $sordata) {\n            if (array_key_exists($childname, $sordata) &amp;&amp; !empty($sordata[$childname])) {\n                $res = deal_list_to_tree2($data, $pkname, $pidname, $childname, $is_empty_childrens, $sordata[$pkname]);\n            } else {\n                if ($sordata[$pidname] == $rootid) {\n                    if ($sordata[$pkname] != $rootid) {\n                        $res = deal_list_to_tree2($data, $pkname, $pidname, $childname, $is_empty_childrens, $sordata[$pkname]);\n                    }\n                    if (!empty($res) &amp;&amp; !$is_empty_childrens) {\n                        if (array_key_exists($childname, $sordata)) {\n                            if (array_key_exists($childname, $sordata)) {\n                                for ($i = 0; $i &lt; count($res); $i++) {\n                                    $sordata[$childname][] = $res[$i];\n                                }\n                            } else {\n                                $sordata[$childname][] = $res;\n                            }\n                        } else {\n                            $sordata[$childname] = $res;\n                        }\n                    }\n                    $new_data[] = $sordata;\n                }\n            }\n        }\n    }\n    return $new_data;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8c03\u7528 deal_list_to_tree2 \u51fd\u6570\u53ef\u4ee5\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u591a\u7ef4\u6570\u7ec4\uff1a<\/p>\n<pre>$multidimensionalData = deal_list_to_tree2($result);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u73b0\u5728\uff0c\u4f60\u5c06\u62e5\u6709\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5b50\u5206\u7c7b\u7684\u591a\u7ef4\u6570\u7ec4\u3002\u8bf7\u6ce8\u610f\uff0cdeal_list_to_tree2 \u51fd\u6570\u662f\u4e00\u4e2a\u901a\u7528\u7684\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u7528\u4e8e\u8f6c\u6362\u4efb\u4f55\u4e00\u7ef4\u6570\u7ec4\u5230\u591a\u7ef4\u6570\u7ec4\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fThinkPHP\u6846\u67b6\u4e2d\u5982\u4f55\u5c06\u65e0\u9650\u7ea7\u5206\u7c7b\u7684\u4e00\u7ef4\u6570\u7ec4\u8f6c\u6362\u4e3a\u591a\u7ef4\u6570\u7ec4\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u65e0\u9650\u7ea7\u5206\u7c7b\u5b50\u5206\u7c7b\u8bfb\u53d6 \u95ee\u9898\uff1a \u5982\u4f55\u4f7f\u7528 \u6846\u67b6\u8bfb\u53d6\u65e0\u9650\u7ea7\u5206\u7c7b\u4e2d\u7684\u6240\u6709\u5b50\u5206\u7c7b\uff0c\u5e76\u4ee5\u591a\u7ef4\u6570\u7ec4\u5f62\u5f0f\u8fd4\u56de\uff1f \u89e3\u51b3\u65b9\u6848\uff1a \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u9996\u5148\uff0c\u8981\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u51fd\u6570 getchildarea \u6765\u8bfb\u53d6\u5b50\u5206\u7c7b\uff0c\u8be5\u51fd\u6570\u5c06\u4ee5\u7ed9\u5b9a\u7684\u5730\u533a id \u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u4ee5\u9012\u5f52\u65b9\u5f0f\u904d\u5386\u6240\u6709\u5b50\u5206\u7c7b\u3002 function getchildarea($id) { if (!$id) { return; } static $area; $area = $area ?? new ppcommonmodelrea; $result = collection($area-&gt;where([&#8216;pid&#8217; =&gt; $id])-&gt;order(&#8216;id desc&#8217;)-&gt;select())-&gt;toarray(); static $res = []; if ($result) { foreach ($result as $key =&gt; $val) { $res[] = $val; getchildarea($val[&#8216;id&#8217;]); } } return $res; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-26077","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/26077","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=26077"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/26077\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=26077"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=26077"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=26077"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}