{"id":47433,"date":"2024-12-02T12:57:54","date_gmt":"2024-12-02T04:57:54","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47433\/"},"modified":"2024-12-02T12:57:54","modified_gmt":"2024-12-02T04:57:54","slug":"php-%e5%87%bd%e6%95%b0%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e9%80%92%e5%bd%92%e6%9d%a5%e6%9e%84%e5%bb%ba%e6%a0%91%e5%bd%a2%e7%bb%93%e6%9e%84%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47433\/","title":{"rendered":"PHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u6784\u5efa\u6811\u5f62\u7ed3\u6784\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u6784\u5efa\u6811\u5f62\u7ed3\u6784\uff1f<\/h1>\n<p>\u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300aPHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u6784\u5efa\u6811\u5f62\u7ed3\u6784\uff1f\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002<\/p>\n<p>\u5728 PHP \u51fd\u6570\u4e2d\u4f7f\u7528\u9012\u5f52\u53ef\u4ee5\u6784\u5efa\u6811\u5f62\u7ed3\u6784\uff0c\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a\u5b9a\u4e49\u4e00\u4e2a\u8868\u793a\u8282\u70b9\u7684\u7c7b\u6216\u5bf9\u8c61\uff1b\u521b\u5efa\u4e00\u4e2a\u9012\u5f52\u51fd\u6570\u6765\u6784\u5efa\u6811\uff1b\u901a\u8fc7\u5b9e\u6218\u4f8b\u5b50\u5c55\u793a\u5982\u4f55\u5c06 JSON \u6570\u7ec4\u6570\u636e\u8f6c\u6362\u4e3a\u6811\u7ed3\u6784\uff1b\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u904d\u5386\u548c\u8bbf\u95ee\u6811\u7ed3\u6784\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729917931671c73eb73a2c.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u6784\u5efa\u6811\u5f62\u7ed3\u6784\uff1f\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u6784\u5efa\u6811\u5f62\u7ed3\u6784\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5728 PHP \u51fd\u6570\u4e2d\u4f7f\u7528\u9012\u5f52\u6784\u5efa\u6811\u5f62\u7ed3\u6784<\/strong><\/p>\n<p>\u9012\u5f52\u662f\u4e00\u79cd\u5f3a\u5927\u4e14\u5b9e\u7528\u7684\u7f16\u7a0b\u6280\u672f\uff0c\u5b83\u5141\u8bb8\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u6765\u89e3\u51b3\u4efb\u52a1\u3002\u5728\u6784\u5efa\u6811\u5f62\u7ed3\u6784\u65f6\uff0c\u9012\u5f52\u7279\u522b\u6709\u7528\uff0c\u5176\u4e2d\u6bcf\u4e2a\u8282\u70b9\u90fd\u53ef\u4ee5\u6709\u5b50\u8282\u70b9\u3002<\/p>\n<p><strong>\u6b65\u9aa4\uff1a<\/strong><\/p>\n<ol>\n<li>\n<p><strong>\u5b9a\u4e49\u4e00\u4e2a\u8868\u793a\u8282\u70b9\u7684\u7c7b\u6216\u5bf9\u8c61\uff1a<\/strong><\/p>\n<pre>class Node {\n    private $value;\n    private $children = [];\n\n    public function __construct($value) {\n        $this-&gt;value = $value;\n    }\n\n    public function getValue() {\n        return $this-&gt;value;\n    }\n\n    public function addChild(Node $child) {\n        $this-&gt;children[] = $child;\n    }\n\n    public function getChildren() {\n        return $this-&gt;children;\n    }\n}<\/pre>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u4e00\u4e2a\u9012\u5f52\u51fd\u6570\u6765\u6784\u5efa\u6811\uff1a<\/strong><\/p>\n<pre>function buildTree($data) {\n    if (empty($data)) {\n        return null;\n    }\n\n    $root = new Node($data[0]);\n\n    foreach ($data[1] as $childData) {\n        $child = buildTree($childData);\n        if ($child) {\n            $root-&gt;addChild($child);\n        }\n    }\n\n    return $root;\n}<\/pre>\n<\/li>\n<\/ol>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a JSON \u6570\u7ec4\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a\u6811\u5f62\u7ed3\u6784\u6570\u636e\uff1a<\/p>\n<pre>{\n    \"name\": \"Root\",\n    \"children\": [\n        {\n            \"name\": \"Child A\",\n            \"children\": [\n                {\n                    \"name\": \"Child AA\"\n                },\n                {\n                    \"name\": \"Child AB\"\n                }\n            ]\n        },\n        {\n            \"name\": \"Child B\",\n            \"children\": [\n                {\n                    \"name\": \"Child BA\"\n                }\n            ]\n        }\n    ]\n}<\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>buildTree()<\/code> \u51fd\u6570\u5c06\u6b64\u6570\u636e\u8f6c\u6362\u4e3a\u4e00\u4e2a\u6811\u7ed3\u6784\uff1a<\/p>\n<pre>$data = json_decode($json, true);\n$tree = buildTree($data);<\/pre>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6811\u7ed3\u6784\u6765\u904d\u5386\u548c\u8bbf\u95ee\u6570\u636e\u3002\u4f8b\u5982\uff0c\u8981\u83b7\u53d6\u6240\u6709\u5b50\u8282\u70b9\u7684\u540d\u79f0\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u9012\u5f52\u51fd\u6570\uff1a<\/p>\n<pre>function getChildrenNames(Node $node) {\n    $names = [$node-&gt;getValue()];\n    foreach ($node-&gt;getChildren() as $child) {\n        $names = array_merge($names, getChildrenNames($child));\n    }\n    return $names;\n}<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300aPHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u6784\u5efa\u6811\u5f62\u7ed3\u6784\uff1f\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u9012\u5f52,\u6784\u5efa\u6811\u5f62\u7ed3\u6784\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u6784\u5efa\u6811\u5f62\u7ed3\u6784\uff1f \u4eb2\u7231\u7684\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5982\u679c\u4f60\u70b9\u5f00\u4e86\u8fd9\u7bc7\u6587\u7ae0\uff0c\u8bf4\u660e\u4f60\u5bf9\u300aPHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u6784\u5efa\u6811\u5f62\u7ed3\u6784\uff1f\u300b\u5f88\u611f\u5174\u8da3\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u7ed9\u5927\u5bb6\u8be6\u7ec6\u89e3\u6790\u4e00\u4e0b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u6240\u6709\u8ba4\u771f\u8bfb\u5b8c\u7684\u7ae5\u978b\u4eec\uff0c\u90fd\u6709\u5b9e\u8d28\u6027\u7684\u63d0\u9ad8\u3002 \u5728 PHP \u51fd\u6570\u4e2d\u4f7f\u7528\u9012\u5f52\u53ef\u4ee5\u6784\u5efa\u6811\u5f62\u7ed3\u6784\uff0c\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a\u5b9a\u4e49\u4e00\u4e2a\u8868\u793a\u8282\u70b9\u7684\u7c7b\u6216\u5bf9\u8c61\uff1b\u521b\u5efa\u4e00\u4e2a\u9012\u5f52\u51fd\u6570\u6765\u6784\u5efa\u6811\uff1b\u901a\u8fc7\u5b9e\u6218\u4f8b\u5b50\u5c55\u793a\u5982\u4f55\u5c06 JSON \u6570\u7ec4\u6570\u636e\u8f6c\u6362\u4e3a\u6811\u7ed3\u6784\uff1b\u4f7f\u7528\u9012\u5f52\u51fd\u6570\u904d\u5386\u548c\u8bbf\u95ee\u6811\u7ed3\u6784\u4e2d\u7684\u6570\u636e\u3002 \u5728 PHP \u51fd\u6570\u4e2d\u4f7f\u7528\u9012\u5f52\u6784\u5efa\u6811\u5f62\u7ed3\u6784 \u9012\u5f52\u662f\u4e00\u79cd\u5f3a\u5927\u4e14\u5b9e\u7528\u7684\u7f16\u7a0b\u6280\u672f\uff0c\u5b83\u5141\u8bb8\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u6765\u89e3\u51b3\u4efb\u52a1\u3002\u5728\u6784\u5efa\u6811\u5f62\u7ed3\u6784\u65f6\uff0c\u9012\u5f52\u7279\u522b\u6709\u7528\uff0c\u5176\u4e2d\u6bcf\u4e2a\u8282\u70b9\u90fd\u53ef\u4ee5\u6709\u5b50\u8282\u70b9\u3002 \u6b65\u9aa4\uff1a \u5b9a\u4e49\u4e00\u4e2a\u8868\u793a\u8282\u70b9\u7684\u7c7b\u6216\u5bf9\u8c61\uff1a class Node { private $value; private $children = []; public function __construct($value) { $this-&gt;value = $value; } public function getValue() { return $this-&gt;value; } public function addChild(Node $child) { $this-&gt;children[] = $child; } public function getChildren() { return $this-&gt;children; } [&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-47433","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47433","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=47433"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47433\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}