{"id":7413,"date":"2024-11-14T10:24:35","date_gmt":"2024-11-14T02:24:35","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7413\/"},"modified":"2024-11-14T10:24:35","modified_gmt":"2024-11-14T02:24:35","slug":"thinkphp%e6%a1%86%e6%9e%b6%e5%a6%82%e4%bd%95%e5%b0%86%e9%80%92%e5%bd%92%e8%8e%b7%e5%8f%96%e7%9a%84%e6%97%a0%e9%99%90%e7%ba%a7%e5%88%86%e7%b1%bb%e5%ad%90%e5%88%86%e7%b1%bb%e6%95%b0%e6%8d%ae%e8%bd%ac","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/7413\/","title":{"rendered":"ThinkPHP\u6846\u67b6\u5982\u4f55\u5c06\u9012\u5f52\u83b7\u53d6\u7684\u65e0\u9650\u7ea7\u5206\u7c7b\u5b50\u5206\u7c7b\u6570\u636e\u8f6c\u5316\u4e3a\u591a\u7ef4\u6570\u7ec4\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173080036963282.jpg\" class=\"aligncenter\" title=\"ThinkPHP\u6846\u67b6\u5982\u4f55\u5c06\u9012\u5f52\u83b7\u53d6\u7684\u65e0\u9650\u7ea7\u5206\u7c7b\u5b50\u5206\u7c7b\u6570\u636e\u8f6c\u5316\u4e3a\u591a\u7ef4\u6570\u7ec4\uff1f\u63d2\u56fe\" alt=\"ThinkPHP\u6846\u67b6\u5982\u4f55\u5c06\u9012\u5f52\u83b7\u53d6\u7684\u65e0\u9650\u7ea7\u5206\u7c7b\u5b50\u5206\u7c7b\u6570\u636e\u8f6c\u5316\u4e3a\u591a\u7ef4\u6570\u7ec4\uff1f\u63d2\u56fe\" \/><\/p>\n<p>\u5728\u6846\u67b6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u7684\u65b9\u5f0f\u8bfb\u53d6\u65e0\u9650\u7ea7\u5206\u7c7b\u7684\u5b50\u5206\u7c7b\u3002\u4f7f\u7528getchildarea()\u51fd\u6570\u65f6\uff0c\u8fd4\u56de\u7684\u662f\u4e00\u7ef4\u6570\u7ec4\uff0c\u5982\u679c\u60a8\u9700\u8981\u591a\u7ef4\u6570\u7ec4\uff0c\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u65b9\u5f0f\u6539\u8fdb\uff1a<\/p>\n<pre>function getchildarea($id){\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            $val['children'] = getchildarea($val['id']); \n            $res[] = $val;\n        }\n    }\n    return $res;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u914d\u5408deal_list_to_tree2()\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u5316\u4e3a\u591a\u7ef4\u6570\u7ec4\uff1a<\/p>\n<pre>&lt;?php\n\/**\n * \u65b9\u6cd5 deal_list_to_tree2\uff0c\u4e00\u7ef4\u6570\u7ec4\u6839\u636e$parent_id\u7684\u503c\u8f6c\u4e3a\u591a\u7ef4\u6570\u7ec4\n *\n * @param array $data \u5f85\u5904\u7406\u7684\u4e00\u7ef4\u6570\u7ec4\n * @param string $pkname \u7528\u4e8e\u8f6c\u5316\u4e3a\u591a\u7ef4\u6570\u7ec4\u7684\u4e3b\u952e\u5b57\u6bb5\n * @param string $pidname \u7528\u4e8e\u8f6c\u5316\u4e3a\u591a\u7ef4\u6570\u7ec4\u7684\u5b57\u6bb5\uff08\u6839\u636e\u8be5\u5b57\u6bb5\u503c\u8f6c\u6362\uff09\n * @param string $childname \u5b50\u7ea7\u7684\u5b57\u6bb5\u540d\n * @param bool $is_empty_childrens \u662f\u5426\u8fd4\u56de\u7a7a\u7684\u5b50\u6570\u7ec4\uff08childrens[]\uff09\uff08true:\u662f\uff0cfalse\uff1a\u5426\uff09\n * @param string $rootid \u6839\u8282\u70b9$pkname\u503c\n *\n * @return array $new_data \u8fd4\u56de\u5904\u7406\u597d\u7684\uff08\u591a\u5c42\u7ea7\uff09\u591a\u7ef4\u6570\u7ec4\n *\n *\/\nfunction deal_list_to_tree2($data, $pkname='id', $pidname='parent_id', $childname='children_list', $is_empty_childrens=false, $rootid=''){\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>\u8fd9\u6837\uff0c\u8fd4\u56de\u7684\u591a\u7ef4\u6570\u7ec4\u683c\u5f0f\u4e3a\uff1a<\/p>\n<pre>[\n  {\n    \"id\": 1,\n    \"area_name\": \"\u5b89\u5fbd\u7701\",\n    \"pid\": 0,\n    \"level\": 1,\n    \"children\": [\n      {\n        \"id\": 4,\n        \"area_name\": \"\u5408\u80a5\u5e02\",\n        \"pid\": 1,\n        \"level\": 2,\n        \"children\": [\n          {\n            \"id\": 7,\n            \"area_name\": \"\u80a5\u4e1c\u53bf\",\n            \"pid\": 4,\n            \"level\": 3,\n            \"children\": [\n              {\n                \"id\": 8,\n                \"area_name\": \"\u6843\u56ed\u9547\",\n                \"pid\": 7,\n                \"level\": 4,\n                \"children\": [\n                  {\n                    \"id\": 9,\n                    \"area_name\": \"\u516b\u6597\u4e61\",\n                    \"pid\": 8,\n                    \"level\": 5\n                  }\n                ]\n              },\n              {\n                \"id\": 10,\n                \"area_name\": \"\u957f\u4e30\u53bf\",\n                \"pid\": 4,\n                \"level\": 3\n              }\n            ]\n          }\n        ]\n      }\n    ]\n  },\n  {\n    \"id\": 2,\n    \"area_name\": \"\u6c5f\u82cf\u7701\",\n    \"pid\": 0,\n    \"level\": 1\n  },\n  {\n    \"id\": 3,\n    \"area_name\": \"\u6c5f\u897f\u7701\",\n    \"pid\": 0,\n    \"level\": 1,\n    \"children\": [\n      {\n        \"id\": 5,\n        \"area_name\": \"\u5357\u660c\u5e02\",\n        \"pid\": 3,\n        \"level\": 2\n      },\n      {\n        \"id\": 6,\n        \"area_name\": \"\u4e5d\u6c5f\u5e02\",\n        \"pid\": 3,\n        \"level\": 2\n      }\n    ]\n  }\n]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fThinkPHP\u6846\u67b6\u5982\u4f55\u5c06\u9012\u5f52\u83b7\u53d6\u7684\u65e0\u9650\u7ea7\u5206\u7c7b\u5b50\u5206\u7c7b\u6570\u636e\u8f6c\u5316\u4e3a\u591a\u7ef4\u6570\u7ec4\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u6846\u67b6\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u9012\u5f52\u7684\u65b9\u5f0f\u8bfb\u53d6\u65e0\u9650\u7ea7\u5206\u7c7b\u7684\u5b50\u5206\u7c7b\u3002\u4f7f\u7528getchildarea()\u51fd\u6570\u65f6\uff0c\u8fd4\u56de\u7684\u662f\u4e00\u7ef4\u6570\u7ec4\uff0c\u5982\u679c\u60a8\u9700\u8981\u591a\u7ef4\u6570\u7ec4\uff0c\u53ef\u4ee5\u91c7\u7528\u4ee5\u4e0b\u65b9\u5f0f\u6539\u8fdb\uff1a 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) { $val[&#8216;children&#8217;] = getchildarea($val[&#8216;id&#8217;]); $res[] = $val; } } return $res; } \u767b\u5f55\u540e\u590d\u5236 \u914d\u5408deal_list_to_tree2()\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06\u4e00\u7ef4\u6570\u7ec4\u8f6c\u5316\u4e3a\u591a\u7ef4\u6570\u7ec4\uff1a &lt;?php \/** * \u65b9\u6cd5 deal_list_to_tree2\uff0c\u4e00\u7ef4\u6570\u7ec4\u6839\u636e$parent_id\u7684\u503c\u8f6c\u4e3a\u591a\u7ef4\u6570\u7ec4 * * @param array $data [&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-7413","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7413","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=7413"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7413\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}