{"id":48198,"date":"2024-12-02T13:21:27","date_gmt":"2024-12-02T05:21:27","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48198\/"},"modified":"2024-12-02T13:21:27","modified_gmt":"2024-12-02T05:21:27","slug":"php-%e5%87%bd%e6%95%b0%e6%80%a7%e8%83%bd%e4%bc%98%e5%8c%96%e4%b8%ad%e7%9a%84%e6%a0%b8%e5%bf%83%e7%ae%97%e6%b3%95%e4%b8%8e%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48198\/","title":{"rendered":"PHP \u51fd\u6570\u6027\u80fd\u4f18\u5316\u4e2d\u7684\u6838\u5fc3\u7b97\u6cd5\u4e0e\u6570\u636e\u7ed3\u6784"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u6027\u80fd\u4f18\u5316\u4e2d\u7684\u6838\u5fc3\u7b97\u6cd5\u4e0e\u6570\u636e\u7ed3\u6784<\/h1>\n<p>\u5c0f\u4f19\u4f34\u4eec\u6709\u6ca1\u6709\u89c9\u5f97\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u5f88\u6709\u610f\u601d\uff1f\u6709\u610f\u601d\u5c31\u5bf9\u4e86\uff01\u4eca\u5929\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u51fd\u6570\u6027\u80fd\u4f18\u5316\u4e2d\u7684\u6838\u5fc3\u7b97\u6cd5\u4e0e\u6570\u636e\u7ed3\u6784\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u5728\u5b66\u4e60\u4e2d\u5bf9\u5176\u4e2d\u90e8\u5206\u77e5\u8bc6\u70b9\u6709\u7591\u95ee\uff0c\u6216\u8bb8\u770b\u4e86\u672c\u6587\u5c31\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<p>\u5728 PHP \u51fd\u6570\u6027\u80fd\u4f18\u5316\u4e2d\uff0c\u9009\u62e9\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u81f3\u5173\u91cd\u8981\u3002\u7b97\u6cd5\u65f6\u95f4\u590d\u6742\u5ea6\u51b3\u5b9a\u64cd\u4f5c\u6b21\u6570\u968f\u6570\u636e\u89c4\u6a21\u7684\u53d8\u5316\u60c5\u51b5\uff0c\u63a8\u8350\u4f7f\u7528\u5e38\u91cf\u6216\u5bf9\u6570\u65f6\u95f4\u7b97\u6cd5\uff1b\u6570\u636e\u7ed3\u6784\u7a7a\u95f4\u590d\u6742\u5ea6\u51b3\u5b9a\u5b58\u50a8\u7a7a\u95f4\u968f\u6570\u636e\u89c4\u6a21\u7684\u53d8\u5316\u60c5\u51b5\uff0c\u63a8\u8350\u4f7f\u7528\u5e38\u91cf\u7a7a\u95f4\u6570\u636e\u7ed3\u6784\u3002\u5982\u4f18\u5316\u6570\u7ec4\u67e5\u627e\u53ef\u4f7f\u7528\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\uff0c\u4f18\u5316\u952e\u503c\u5bf9\u5b58\u50a8\u53ef\u4f7f\u7528\u6563\u5217\u8868\u3002\u901a\u8fc7\u9009\u62e9\u5408\u9002\u7684\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u663e\u8457\u63d0\u9ad8 PHP \u51fd\u6570\u6027\u80fd\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1729997921671dac61adcd4.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u6027\u80fd\u4f18\u5316\u4e2d\u7684\u6838\u5fc3\u7b97\u6cd5\u4e0e\u6570\u636e\u7ed3\u6784\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u6027\u80fd\u4f18\u5316\u4e2d\u7684\u6838\u5fc3\u7b97\u6cd5\u4e0e\u6570\u636e\u7ed3\u6784\u63d2\u56fe\" \/><\/p>\n<h2>PHP \u51fd\u6570\u6027\u80fd\u4f18\u5316\u4e2d\u7684\u6838\u5fc3\u7b97\u6cd5\u4e0e\u6570\u636e\u7ed3\u6784<\/h2>\n<p>\u5728 PHP \u51fd\u6570\u7684\u6027\u80fd\u4f18\u5316\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u81f3\u5173\u91cd\u8981\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u4e00\u4e9b\u5e38\u7528\u7684\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\uff0c\u4ee5\u53ca\u5b83\u4eec\u5728 PHP \u4e2d\u7684\u4f18\u5316\u793a\u4f8b\u3002<\/p>\n<h3>\u7b97\u6cd5<\/h3>\n<p><strong>\u7b97\u6cd5\u65f6\u95f4\u590d\u6742\u5ea6\uff1a<\/strong><\/p>\n<ul>\n<li>O(1): \u5e38\u91cf\u65f6\u95f4\uff0c\u64cd\u4f5c\u6b21\u6570\u4e0d\u968f\u6570\u636e\u89c4\u6a21\u53d8\u5316\u800c\u53d8\u5316\u3002<\/li>\n<li>O(log n): \u5bf9\u6570\u65f6\u95f4\uff0c\u64cd\u4f5c\u6b21\u6570\u4e0e\u6570\u636e\u89c4\u6a21\u7684\u5bf9\u6570\u6210\u6b63\u6bd4\u3002<\/li>\n<li>O(n): \u7ebf\u6027\u65f6\u95f4\uff0c\u64cd\u4f5c\u6b21\u6570\u4e0e\u6570\u636e\u89c4\u6a21\u6210\u6b63\u6bd4\u3002<\/li>\n<li>O(n^2): \u5e73\u65b9\u65f6\u95f4\uff0c\u64cd\u4f5c\u6b21\u6570\u4e0e\u6570\u636e\u89c4\u6a21\u7684\u5e73\u65b9\u6210\u6b63\u6bd4\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<ul>\n<li><strong>\u6570\u7ec4\u67e5\u627e\uff1a<\/strong><code>in_array<\/code> \u7b97\u6cd5\u5177\u6709 O(n) \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u800c\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u5177\u6709 O(log n) \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002\u5982\u679c\u6570\u7ec4\u5f88\u5927\uff0c\u4e8c\u5206\u67e5\u627e\u4f1a\u66f4\u6709\u6548\u3002<\/li>\n<\/ul>\n<h3>\u6570\u636e\u7ed3\u6784<\/h3>\n<p><strong>\u6570\u636e\u7ed3\u6784\u7a7a\u95f4\u590d\u6742\u5ea6\uff1a<\/strong><\/p>\n<ul>\n<li>O(1): \u5e38\u91cf\u7a7a\u95f4\uff0c\u5b58\u50a8\u7a7a\u95f4\u4e0d\u968f\u6570\u636e\u89c4\u6a21\u53d8\u5316\u3002<\/li>\n<li>O(n): \u7ebf\u6027\u7a7a\u95f4\uff0c\u5b58\u50a8\u7a7a\u95f4\u4e0e\u6570\u636e\u89c4\u6a21\u6210\u6b63\u6bd4\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<ul>\n<li><strong>\u952e\u503c\u5bf9\u5b58\u50a8\uff1a<\/strong>\u6563\u5217\u8868\uff08<code>HashMap<\/code>\uff09\u5177\u6709 O(1) \u7684\u7a7a\u95f4\u590d\u6742\u5ea6\uff0c\u800c\u5173\u8054\u6570\u7ec4\uff08<code>Array<\/code>\uff09\u5177\u6709 O(n) \u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u3002\u5982\u679c\u952e\u503c\u5bf9\u6570\u91cf\u5f88\u5927\uff0c\u6563\u5217\u8868\u4f1a\u66f4\u8282\u7701\u7a7a\u95f4\u3002<\/li>\n<\/ul>\n<h3>\u5b9e\u6218\u6848\u4f8b<\/h3>\n<p><strong>\u793a\u4f8b 1\uff1a\u4f18\u5316\u6570\u7ec4\u67e5\u627e<\/strong><\/p>\n<p>\u4f7f\u7528\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u66ff\u6362 <code>in_array<\/code> \u51fd\u6570\uff1a<\/p>\n<pre>function binarySearch($arr, $key) {\n  $low = 0;\n  $high = count($arr) - 1;\n\n  while ($low &lt;= $high) {\n    $mid = floor(($low + $high) \/ 2);\n    if ($arr[$mid] == $key) {\n      return true;\n    } elseif ($arr[$mid] &gt; $key) {\n      $high = $mid - 1;\n    } else {\n      $low = $mid + 1;\n    }\n  }\n\n  return false;\n}<\/pre>\n<p><strong>\u793a\u4f8b 2\uff1a\u4f18\u5316\u952e\u503c\u5bf9\u5b58\u50a8<\/strong><\/p>\n<p>\u4f7f\u7528\u6563\u5217\u8868\u66ff\u6362\u5173\u8054\u6570\u7ec4\uff1a<\/p>\n<pre>class HashMap {\n  private $data = [];\n\n  public function set($key, $value) {\n    $hash = sha1($key);\n    $data[$hash] = $value;\n  }\n\n  public function get($key) {\n    $hash = sha1($key);\n    return $data[$hash] ?? null;\n  }\n}<\/pre>\n<p>\u901a\u8fc7\u9009\u62e9\u6b63\u786e\u7684\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8 PHP \u51fd\u6570\u7684\u6027\u80fd\u3002\u6839\u636e\u6570\u636e\u7279\u5f81\u5408\u7406\u4f7f\u7528\u4e8c\u5206\u67e5\u627e\u3001\u6563\u5217\u8868\u7b49\u6280\u672f\uff0c\u53ef\u4ee5\u5b9e\u73b0\u66f4\u5feb\u7684\u6267\u884c\u901f\u5ea6\u548c\u66f4\u4f4e\u7684\u8d44\u6e90\u6d88\u8017\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aPHP \u51fd\u6570\u6027\u80fd\u4f18\u5316\u4e2d\u7684\u6838\u5fc3\u7b97\u6cd5\u4e0e\u6570\u636e\u7ed3\u6784\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u6027\u80fd\u4f18\u5316\u4e2d\u7684\u6838\u5fc3\u7b97\u6cd5\u4e0e\u6570\u636e\u7ed3\u6784 \u5c0f\u4f19\u4f34\u4eec\u6709\u6ca1\u6709\u89c9\u5f97\u5b66\u4e60\u6587\u7ae0\u5f88\u6709\u610f\u601d\uff1f\u6709\u610f\u601d\u5c31\u5bf9\u4e86\uff01\u4eca\u5929\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765\u300aPHP \u51fd\u6570\u6027\u80fd\u4f18\u5316\u4e2d\u7684\u6838\u5fc3\u7b97\u6cd5\u4e0e\u6570\u636e\u7ed3\u6784\u300b\uff0c\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230\uff0c\u82e5\u662f\u5728\u5b66\u4e60\u4e2d\u5bf9\u5176\u4e2d\u90e8\u5206\u77e5\u8bc6\u70b9\u6709\u7591\u95ee\uff0c\u6216\u8bb8\u770b\u4e86\u672c\u6587\u5c31\u80fd\u5e2e\u5230\u4f60\uff01 \u5728 PHP \u51fd\u6570\u6027\u80fd\u4f18\u5316\u4e2d\uff0c\u9009\u62e9\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u81f3\u5173\u91cd\u8981\u3002\u7b97\u6cd5\u65f6\u95f4\u590d\u6742\u5ea6\u51b3\u5b9a\u64cd\u4f5c\u6b21\u6570\u968f\u6570\u636e\u89c4\u6a21\u7684\u53d8\u5316\u60c5\u51b5\uff0c\u63a8\u8350\u4f7f\u7528\u5e38\u91cf\u6216\u5bf9\u6570\u65f6\u95f4\u7b97\u6cd5\uff1b\u6570\u636e\u7ed3\u6784\u7a7a\u95f4\u590d\u6742\u5ea6\u51b3\u5b9a\u5b58\u50a8\u7a7a\u95f4\u968f\u6570\u636e\u89c4\u6a21\u7684\u53d8\u5316\u60c5\u51b5\uff0c\u63a8\u8350\u4f7f\u7528\u5e38\u91cf\u7a7a\u95f4\u6570\u636e\u7ed3\u6784\u3002\u5982\u4f18\u5316\u6570\u7ec4\u67e5\u627e\u53ef\u4f7f\u7528\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\uff0c\u4f18\u5316\u952e\u503c\u5bf9\u5b58\u50a8\u53ef\u4f7f\u7528\u6563\u5217\u8868\u3002\u901a\u8fc7\u9009\u62e9\u5408\u9002\u7684\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u663e\u8457\u63d0\u9ad8 PHP \u51fd\u6570\u6027\u80fd\u3002 PHP \u51fd\u6570\u6027\u80fd\u4f18\u5316\u4e2d\u7684\u6838\u5fc3\u7b97\u6cd5\u4e0e\u6570\u636e\u7ed3\u6784 \u5728 PHP \u51fd\u6570\u7684\u6027\u80fd\u4f18\u5316\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\u81f3\u5173\u91cd\u8981\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u4e00\u4e9b\u5e38\u7528\u7684\u7b97\u6cd5\u548c\u6570\u636e\u7ed3\u6784\uff0c\u4ee5\u53ca\u5b83\u4eec\u5728 PHP \u4e2d\u7684\u4f18\u5316\u793a\u4f8b\u3002 \u7b97\u6cd5 \u7b97\u6cd5\u65f6\u95f4\u590d\u6742\u5ea6\uff1a O(1): \u5e38\u91cf\u65f6\u95f4\uff0c\u64cd\u4f5c\u6b21\u6570\u4e0d\u968f\u6570\u636e\u89c4\u6a21\u53d8\u5316\u800c\u53d8\u5316\u3002 O(log n): \u5bf9\u6570\u65f6\u95f4\uff0c\u64cd\u4f5c\u6b21\u6570\u4e0e\u6570\u636e\u89c4\u6a21\u7684\u5bf9\u6570\u6210\u6b63\u6bd4\u3002 O(n): \u7ebf\u6027\u65f6\u95f4\uff0c\u64cd\u4f5c\u6b21\u6570\u4e0e\u6570\u636e\u89c4\u6a21\u6210\u6b63\u6bd4\u3002 O(n^2): \u5e73\u65b9\u65f6\u95f4\uff0c\u64cd\u4f5c\u6b21\u6570\u4e0e\u6570\u636e\u89c4\u6a21\u7684\u5e73\u65b9\u6210\u6b63\u6bd4\u3002 \u793a\u4f8b\uff1a \u6570\u7ec4\u67e5\u627e\uff1ain_array \u7b97\u6cd5\u5177\u6709 O(n) \u7684\u65f6\u95f4\u590d\u6742\u5ea6\uff0c\u800c\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u5177\u6709 O(log n) \u7684\u65f6\u95f4\u590d\u6742\u5ea6\u3002\u5982\u679c\u6570\u7ec4\u5f88\u5927\uff0c\u4e8c\u5206\u67e5\u627e\u4f1a\u66f4\u6709\u6548\u3002 \u6570\u636e\u7ed3\u6784 \u6570\u636e\u7ed3\u6784\u7a7a\u95f4\u590d\u6742\u5ea6\uff1a O(1): \u5e38\u91cf\u7a7a\u95f4\uff0c\u5b58\u50a8\u7a7a\u95f4\u4e0d\u968f\u6570\u636e\u89c4\u6a21\u53d8\u5316\u3002 O(n): \u7ebf\u6027\u7a7a\u95f4\uff0c\u5b58\u50a8\u7a7a\u95f4\u4e0e\u6570\u636e\u89c4\u6a21\u6210\u6b63\u6bd4\u3002 \u793a\u4f8b\uff1a \u952e\u503c\u5bf9\u5b58\u50a8\uff1a\u6563\u5217\u8868\uff08HashMap\uff09\u5177\u6709 O(1) \u7684\u7a7a\u95f4\u590d\u6742\u5ea6\uff0c\u800c\u5173\u8054\u6570\u7ec4\uff08Array\uff09\u5177\u6709 O(n) \u7684\u7a7a\u95f4\u590d\u6742\u5ea6\u3002\u5982\u679c\u952e\u503c\u5bf9\u6570\u91cf\u5f88\u5927\uff0c\u6563\u5217\u8868\u4f1a\u66f4\u8282\u7701\u7a7a\u95f4\u3002 \u5b9e\u6218\u6848\u4f8b \u793a\u4f8b 1\uff1a\u4f18\u5316\u6570\u7ec4\u67e5\u627e \u4f7f\u7528\u4e8c\u5206\u67e5\u627e\u7b97\u6cd5\u66ff\u6362 in_array \u51fd\u6570\uff1a function binarySearch($arr, [&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-48198","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48198","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=48198"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48198\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}