{"id":49443,"date":"2024-12-02T08:02:10","date_gmt":"2024-12-02T00:02:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/49443\/"},"modified":"2024-12-02T08:02:10","modified_gmt":"2024-12-02T00:02:10","slug":"php%e6%97%a0%e9%99%90%e6%9e%81%e5%88%86%e7%b1%bb%ef%bc%9a%e5%a6%82%e4%bd%95%e7%94%a8%e9%80%92%e5%bd%92%e7%ae%97%e6%b3%95%e5%ae%9e%e7%8e%b0%e7%88%b6%e5%ad%90%e7%ba%a7%e5%88%86%e7%b1%bb%e7%9a%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/49443\/","title":{"rendered":"PHP\u65e0\u9650\u6781\u5206\u7c7b\uff1a\u5982\u4f55\u7528\u9012\u5f52\u7b97\u6cd5\u5b9e\u73b0\u7236\u5b50\u7ea7\u5206\u7c7b\u7684\u904d\u5386\u4e0e\u5c55\u793a\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP\u65e0\u9650\u6781\u5206\u7c7b\uff1a\u5982\u4f55\u7528\u9012\u5f52\u7b97\u6cd5\u5b9e\u73b0\u7236\u5b50\u7ea7\u5206\u7c7b\u7684\u904d\u5386\u4e0e\u5c55\u793a\uff1f<\/h1>\n<p><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP\u65e0\u9650\u6781\u5206\u7c7b\uff1a\u5982\u4f55\u7528\u9012\u5f52\u7b97\u6cd5\u5b9e\u73b0\u7236\u5b50\u7ea7\u5206\u7c7b\u7684\u904d\u5386\u4e0e\u5c55\u793a\uff1f\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241202\/1733105202674d1632767f2.jpg\" class=\"aligncenter\" title=\"PHP\u65e0\u9650\u6781\u5206\u7c7b\uff1a\u5982\u4f55\u7528\u9012\u5f52\u7b97\u6cd5\u5b9e\u73b0\u7236\u5b50\u7ea7\u5206\u7c7b\u7684\u904d\u5386\u4e0e\u5c55\u793a\uff1f\u63d2\u56fe\" alt=\"PHP\u65e0\u9650\u6781\u5206\u7c7b\uff1a\u5982\u4f55\u7528\u9012\u5f52\u7b97\u6cd5\u5b9e\u73b0\u7236\u5b50\u7ea7\u5206\u7c7b\u7684\u904d\u5386\u4e0e\u5c55\u793a\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u65e0\u9650\u6781\u5206\u7c7b\u7684php\u4ee3\u7801\u53ca\u601d\u8def<\/strong><\/p>\n<p>\u8981\u5b9e\u73b0\u65e0\u9650\u6781\u5206\u7c7b\uff0c\u9700\u8981\u4e00\u4e2a\u7b97\u6cd5\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u5c31\u662f\u9012\u5f52\u7b97\u6cd5\u3002<\/p>\n<p><strong>\u601d\u8def\uff1a<\/strong><\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5206\u7c7b\u6570\u636e\u7684\u6570\u7ec4\uff0c\u5305\u542b\u7236\u7ea7 id \u548c\u5b50\u7ea7 id\u3002<\/li>\n<li>\u5b9a\u4e49\u4e00\u4e2a\u9012\u5f52\u51fd\u6570\uff0c\u4f9d\u6b21\u904d\u5386\u6bcf\u4e00\u7ea7\u5206\u7c7b\u3002<\/li>\n<li>\u5728\u9012\u5f52\u8fc7\u7a0b\u4e2d\uff0c\u6839\u636e\u7236\u7ea7 id \u83b7\u53d6\u6240\u6709\u5b50\u7ea7\u5206\u7c7b\u3002<\/li>\n<li>\u5c06\u5b50\u7ea7\u5206\u7c7b\u6dfb\u52a0\u5230\u6570\u7ec4\u5e76\u8c03\u7528\u9012\u5f52\u51fd\u6570\u7ee7\u7eed\u904d\u5386\u3002<\/li>\n<\/ol>\n<p><strong>php\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre>&lt;?php\n\/\/ \u5206\u7c7b\u6570\u636e\n$data = [\n    [\n        'id' =&gt; 1,\n        'parent_id' =&gt; 0,\n    ],\n    [\n        'id' =&gt; 2,\n        'parent_id' =&gt; 1,\n    ],\n    [\n        'id' =&gt; 3,\n        'parent_id' =&gt; 1,\n    ],\n    [\n        'id' =&gt; 4,\n        'parent_id' =&gt; 2,\n    ],\n];\n\n\/\/ \u9012\u5f52\u51fd\u6570\nfunction getCategories($data, $parent_id)\n{\n    $categories = [];\n    foreach ($data as $item) {\n        if ($item['parent_id'] == $parent_id) {\n            $categories[] = $item;\n            $categories = array_merge($categories, getCategories($data, $item['id']));\n        }\n    }\n    return $categories;\n}\n\n\/\/ \u83b7\u53d6\u9876\u7ea7\u5206\u7c7b\n$topCategories = getCategories($data, 0);\n\n\/\/ \u8f93\u51fa\u5206\u7c7b\u7ed3\u6784\nforeach ($topCategories as $topCategory) {\n    echo $topCategory['id'] . ': ' . $topCategory['parent_id'] . '&lt;br&gt;';\n    foreach (getCategories($data, $topCategory['id']) as $childCategory) {\n        echo '&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;' . $childCategory['id'] . ': ' . $childCategory['parent_id'] . '&lt;br&gt;';\n    }\n}\n?&gt;<\/pre>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<p>\u5728\u524d\u7aef\u5c55\u793a\u5206\u7c7b\u65f6\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u9700\u8981\u5c06\u6570\u636e\u91cd\u65b0\u7ec4\u7ec7\u6210\u6240\u9700\u7684\u683c\u5f0f\u3002<\/p>\n<p>\u6587\u4e2d\u5173\u4e8e\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300aPHP\u65e0\u9650\u6781\u5206\u7c7b\uff1a\u5982\u4f55\u7528\u9012\u5f52\u7b97\u6cd5\u5b9e\u73b0\u7236\u5b50\u7ea7\u5206\u7c7b\u7684\u904d\u5386\u4e0e\u5c55\u793a\uff1f\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u65e0\u9650\u6781\u5206\u7c7b\uff1a\u5982\u4f55\u7528\u9012\u5f52\u7b97\u6cd5\u5b9e\u73b0\u7236\u5b50\u7ea7\u5206\u7c7b\u7684\u904d\u5386\u4e0e\u5c55\u793a\uff1f \u6587\u7ae0\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u300aPHP\u65e0\u9650\u6781\u5206\u7c7b\uff1a\u5982\u4f55\u7528\u9012\u5f52\u7b97\u6cd5\u5b9e\u73b0\u7236\u5b50\u7ea7\u5206\u7c7b\u7684\u904d\u5386\u4e0e\u5c55\u793a\uff1f\u300b\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01 \u65e0\u9650\u6781\u5206\u7c7b\u7684php\u4ee3\u7801\u53ca\u601d\u8def \u8981\u5b9e\u73b0\u65e0\u9650\u6781\u5206\u7c7b\uff0c\u9700\u8981\u4e00\u4e2a\u7b97\u6cd5\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u5c31\u662f\u9012\u5f52\u7b97\u6cd5\u3002 \u601d\u8def\uff1a \u521b\u5efa\u4e00\u4e2a\u5305\u542b\u5206\u7c7b\u6570\u636e\u7684\u6570\u7ec4\uff0c\u5305\u542b\u7236\u7ea7 id \u548c\u5b50\u7ea7 id\u3002 \u5b9a\u4e49\u4e00\u4e2a\u9012\u5f52\u51fd\u6570\uff0c\u4f9d\u6b21\u904d\u5386\u6bcf\u4e00\u7ea7\u5206\u7c7b\u3002 \u5728\u9012\u5f52\u8fc7\u7a0b\u4e2d\uff0c\u6839\u636e\u7236\u7ea7 id \u83b7\u53d6\u6240\u6709\u5b50\u7ea7\u5206\u7c7b\u3002 \u5c06\u5b50\u7ea7\u5206\u7c7b\u6dfb\u52a0\u5230\u6570\u7ec4\u5e76\u8c03\u7528\u9012\u5f52\u51fd\u6570\u7ee7\u7eed\u904d\u5386\u3002 php\u4ee3\u7801\uff1a &lt;?php \/\/ \u5206\u7c7b\u6570\u636e $data = [ [ &#8216;id&#8217; =&gt; 1, &#8216;parent_id&#8217; =&gt; 0, ], [ &#8216;id&#8217; =&gt; 2, &#8216;parent_id&#8217; =&gt; 1, ], [ &#8216;id&#8217; =&gt; 3, &#8216;parent_id&#8217; =&gt; 1, ], [ &#8216;id&#8217; =&gt; 4, &#8216;parent_id&#8217; =&gt; 2, ], ]; \/\/ [&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-49443","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49443","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=49443"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/49443\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=49443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=49443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=49443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}