{"id":47323,"date":"2024-12-02T10:03:11","date_gmt":"2024-12-02T02:03:11","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47323\/"},"modified":"2024-12-02T10:03:11","modified_gmt":"2024-12-02T02:03:11","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%e5%ae%9e%e7%8e%b0%e5%b9%bf%e5%ba%a6%e4%bc%98%e5%85%88%e6%90%9c%e7%b4%a2%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47323\/","title":{"rendered":"PHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u5b9e\u73b0\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u5b9e\u73b0\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\uff1f<\/h1>\n<p><span style=\"font-size: 15px\">\u5b66\u4e60\u77e5\u8bc6\u8981\u5584\u4e8e\u601d\u8003\uff0c\u601d\u8003\uff0c\u518d\u601d\u8003\uff01\u4eca\u5929\u7c73\u4e91\u5c0f\u7f16\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u5b9e\u73b0\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\uff1f\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u5305\u542b<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u6216\u51c6\u5907\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u5c31\u90fd\u4e0d\u8981\u9519\u8fc7\u672c\u6587\u5566~\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff0c\u80fd\u5e2e\u52a9\u5230\u4f60\u5c31\u66f4\u597d\u4e86\uff01<\/span><\/p>\n<p>\u5728 PHP \u51fd\u6570\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\u901a\u8fc7\u9012\u5f52\u5b9e\u73b0\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\uff08BFS\uff09\uff1a\u58f0\u660e\u4e00\u4e2a\u961f\u5217\u5e76\u653e\u5165\u4f20\u5165\u8282\u70b9\u3002\u5faa\u73af\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff0c\u76f4\u81f3\u961f\u5217\u4e3a\u7a7a\uff1a\u53d6\u51fa\u961f\u5217\u4e2d\u4e00\u4e2a\u8282\u70b9\uff0c\u5e76\u8fdb\u884c\u5904\u7406\u3002\u5c06\u8be5\u8282\u70b9\u7684\u6240\u6709\u90bb\u5c45\u653e\u5165\u961f\u5217\u3002\u672c\u7b97\u6cd5\u5c06\u6309\u4ee5\u4e0b\u987a\u5e8f\u904d\u5386\u6811\u6216\u56fe\uff1a\u6839\u8282\u70b9\u3001\u6240\u6709\u76f8\u90bb\u8282\u70b9\u3001\u6240\u6709\u76f8\u90bb\u8282\u70b9\u7684\u90bb\u5c45\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u81f3\u6240\u6709\u8282\u70b9\u5747\u5df2\u904d\u5386\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729910974671c58bee4fdb.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u5b9e\u73b0\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\uff1f\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u5b9e\u73b0\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u5b9e\u73b0\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22<\/strong><\/p>\n<p>\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\uff08BFS\uff09\u662f\u4e00\u79cd\u904d\u5386\u6811\u6216\u56fe\u4e2d\u7684\u8282\u70b9\u7684\u7b97\u6cd5\uff0c\u5b83\u9075\u5faa\u201c\u5148\u8fdb\u5148\u51fa\u201d\u7684\u539f\u5219\u3002\u8981\u4f7f\u7528\u9012\u5f52\u5728 PHP \u51fd\u6570\u4e2d\u5b9e\u73b0 BFS\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u961f\u5217\u6765\u5b58\u50a8\u5f85\u8bbf\u95ee\u7684\u8282\u70b9\uff0c\u5e76\u91cd\u590d\u4ee5\u4e0b\u6b65\u9aa4\uff0c\u76f4\u5230\u961f\u5217\u4e3a\u7a7a\uff1a<\/p>\n<pre>function bfs($node) {\n    \/\/ \u58f0\u660e\u4e00\u4e2a\u961f\u5217\n    $queue = new SplQueue();\n\n    \/\/ \u5c06\u4f20\u5165\u7684\u8282\u70b9\u653e\u5165\u961f\u5217\n    $queue-&gt;enqueue($node);\n\n    \/\/ \u91cd\u590d\u6b65\u9aa4\uff0c\u76f4\u5230\u961f\u5217\u4e3a\u7a7a\n    while (!$queue-&gt;isEmpty()) {\n        \n        \/\/ \u4ece\u961f\u5217\u4e2d\u53d6\u51fa\u4e00\u4e2a\u8282\u70b9\n        $node = $queue-&gt;dequeue();\n\n        \/\/ \u5bf9\u8282\u70b9\u8fdb\u884c\u5904\u7406\n\n        \/\/ \u5c06\u8282\u70b9\u7684\u6240\u6709\u90bb\u5c45\u653e\u5165\u961f\u5217\n        foreach ($node-&gt;neighbors as $neighbor) {\n            $queue-&gt;enqueue($neighbor);\n        }\n    }\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4e3a\u4e86\u6f14\u793a BFS \u51fd\u6570\u7684\u5b9e\u9645\u7528\u6cd5\uff0c\u6211\u4eec\u8003\u8651\u4e00\u4e2a\u6811\u5f62\u7ed3\u6784\uff0c\u5176\u4e2d\u6bcf\u4e2a\u8282\u70b9\u90fd\u6709\u591a\u4e2a\u5b50\u8282\u70b9\u3002\u4ee5\u4e0b\u4ee3\u7801\u521b\u5efa\u4e00\u4e2a\u8282\u70b9\u7c7b\u548c\u4e00\u4e2a\u5305\u542b\u6811\u7ed3\u6784\u7684\u6570\u7ec4\uff1a<\/p>\n<pre>class Node {\n    public $value;\n    public $children = [];\n}\n\n$tree = [\n    new Node(['value' =&gt; 'root', 'children' =&gt; [\n        new Node(['value' =&gt; 'child1', 'children' =&gt; [\n            new Node(['value' =&gt; 'grandchild1']),\n            new Node(['value' =&gt; 'grandchild2'])\n        ])),\n        new Node(['value' =&gt; 'child2', 'children' =&gt; [\n            new Node(['value' =&gt; 'grandchild3']),\n            new Node(['value' =&gt; 'grandchild4'])\n        ]])\n    ]])\n];<\/pre>\n<p>\u73b0\u5728\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 BFS \u51fd\u6570\u904d\u5386\u6811\u7ed3\u6784\uff1a<\/p>\n<pre>\/\/ \u4ece\u6839\u8282\u70b9\u5f00\u59cb BFS\nbfs($tree[0]);<\/pre>\n<p>BFS \u4f1a\u6309\u7167\u5982\u4e0b\u987a\u5e8f\u904d\u5386\u8282\u70b9\uff1aroot\u3001child1\u3001child2\u3001grandchild1\u3001grandchild2\u3001grandchild3\u3001grandchild4\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300aPHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u5b9e\u73b0\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\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\u4e8ephp,\u9012\u5f52\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\u5b9e\u73b0\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\uff1f \u5b66\u4e60\u77e5\u8bc6\u8981\u5584\u4e8e\u601d\u8003\uff0c\u601d\u8003\uff0c\u518d\u601d\u8003\uff01\u4eca\u5929\u7c73\u4e91\u5c0f\u7f16\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765\u300aPHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u5b9e\u73b0\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\uff1f\u300b\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u5305\u542b\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u6216\u51c6\u5907\u5b66\u4e60\u6587\u7ae0\uff0c\u5c31\u90fd\u4e0d\u8981\u9519\u8fc7\u672c\u6587\u5566~\u8ba9\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff0c\u80fd\u5e2e\u52a9\u5230\u4f60\u5c31\u66f4\u597d\u4e86\uff01 \u5728 PHP \u51fd\u6570\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\u901a\u8fc7\u9012\u5f52\u5b9e\u73b0\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\uff08BFS\uff09\uff1a\u58f0\u660e\u4e00\u4e2a\u961f\u5217\u5e76\u653e\u5165\u4f20\u5165\u8282\u70b9\u3002\u5faa\u73af\u6267\u884c\u4ee5\u4e0b\u6b65\u9aa4\uff0c\u76f4\u81f3\u961f\u5217\u4e3a\u7a7a\uff1a\u53d6\u51fa\u961f\u5217\u4e2d\u4e00\u4e2a\u8282\u70b9\uff0c\u5e76\u8fdb\u884c\u5904\u7406\u3002\u5c06\u8be5\u8282\u70b9\u7684\u6240\u6709\u90bb\u5c45\u653e\u5165\u961f\u5217\u3002\u672c\u7b97\u6cd5\u5c06\u6309\u4ee5\u4e0b\u987a\u5e8f\u904d\u5386\u6811\u6216\u56fe\uff1a\u6839\u8282\u70b9\u3001\u6240\u6709\u76f8\u90bb\u8282\u70b9\u3001\u6240\u6709\u76f8\u90bb\u8282\u70b9\u7684\u90bb\u5c45\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u76f4\u81f3\u6240\u6709\u8282\u70b9\u5747\u5df2\u904d\u5386\u3002 PHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u5b9e\u73b0\u5e7f\u5ea6\u4f18\u5148\u641c\u7d22 \u5e7f\u5ea6\u4f18\u5148\u641c\u7d22\uff08BFS\uff09\u662f\u4e00\u79cd\u904d\u5386\u6811\u6216\u56fe\u4e2d\u7684\u8282\u70b9\u7684\u7b97\u6cd5\uff0c\u5b83\u9075\u5faa\u201c\u5148\u8fdb\u5148\u51fa\u201d\u7684\u539f\u5219\u3002\u8981\u4f7f\u7528\u9012\u5f52\u5728 PHP \u51fd\u6570\u4e2d\u5b9e\u73b0 BFS\uff0c\u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u961f\u5217\u6765\u5b58\u50a8\u5f85\u8bbf\u95ee\u7684\u8282\u70b9\uff0c\u5e76\u91cd\u590d\u4ee5\u4e0b\u6b65\u9aa4\uff0c\u76f4\u5230\u961f\u5217\u4e3a\u7a7a\uff1a function bfs($node) { \/\/ \u58f0\u660e\u4e00\u4e2a\u961f\u5217 $queue = new SplQueue(); \/\/ \u5c06\u4f20\u5165\u7684\u8282\u70b9\u653e\u5165\u961f\u5217 $queue-&gt;enqueue($node); \/\/ \u91cd\u590d\u6b65\u9aa4\uff0c\u76f4\u5230\u961f\u5217\u4e3a\u7a7a while (!$queue-&gt;isEmpty()) { \/\/ \u4ece\u961f\u5217\u4e2d\u53d6\u51fa\u4e00\u4e2a\u8282\u70b9 $node = $queue-&gt;dequeue(); \/\/ \u5bf9\u8282\u70b9\u8fdb\u884c\u5904\u7406 \/\/ \u5c06\u8282\u70b9\u7684\u6240\u6709\u90bb\u5c45\u653e\u5165\u961f\u5217 foreach ($node-&gt;neighbors as $neighbor) { $queue-&gt;enqueue($neighbor); } } } \u5b9e\u6218\u6848\u4f8b \u4e3a\u4e86\u6f14\u793a BFS \u51fd\u6570\u7684\u5b9e\u9645\u7528\u6cd5\uff0c\u6211\u4eec\u8003\u8651\u4e00\u4e2a\u6811\u5f62\u7ed3\u6784\uff0c\u5176\u4e2d\u6bcf\u4e2a\u8282\u70b9\u90fd\u6709\u591a\u4e2a\u5b50\u8282\u70b9\u3002\u4ee5\u4e0b\u4ee3\u7801\u521b\u5efa\u4e00\u4e2a\u8282\u70b9\u7c7b\u548c\u4e00\u4e2a\u5305\u542b\u6811\u7ed3\u6784\u7684\u6570\u7ec4\uff1a class Node { [&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-47323","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47323","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=47323"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47323\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}