{"id":3031,"date":"2025-03-19T22:20:15","date_gmt":"2025-03-19T14:20:15","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3031"},"modified":"2025-03-19T22:20:15","modified_gmt":"2025-03-19T14:20:15","slug":"php-%e5%a4%9a%e7%bb%b4%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/19\/php-%e5%a4%9a%e7%bb%b4%e6%95%b0%e7%bb%84\/","title":{"rendered":"PHP \u591a\u7ef4\u6570\u7ec4"},"content":{"rendered":"\n<p>PHP \u591a\u7ef4\u6570\u7ec4\u662f\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u672c\u8eab\u662f\u6570\u7ec4\u7684\u6570\u7ec4\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u6570\u7ec4\u7684\u503c\u53ef\u4ee5\u662f\u53e6\u4e00\u4e2a\u6570\u7ec4\uff0c\u56e0\u6b64\u53ef\u4ee5\u521b\u5efa\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002\u591a\u7ef4\u6570\u7ec4\u5728\u5904\u7406\u66f4\u590d\u6742\u7684\u6570\u636e\u65f6\u975e\u5e38\u6709\u7528\uff0c\u6bd4\u5982\u8868\u683c\u6570\u636e\u3001\u77e9\u9635\u3001\u6216\u4efb\u4f55\u5177\u6709\u5c42\u6b21\u7ed3\u6784\u7684\u6570\u636e\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1\ufe0f\u20e3 \u521b\u5efa\u591a\u7ef4\u6570\u7ec4<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.1 \u6570\u7ec4\u7d22\u5f15\u548c\u5173\u8054\u6570\u7ec4<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7d22\u5f15\u6570\u7ec4\uff08Numerical Arrays\uff09\uff1a<\/strong> \u4f7f\u7528\u6570\u5b57\u4f5c\u4e3a\u952e\u6765\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u3002<\/li>\n\n\n\n<li><strong>\u5173\u8054\u6570\u7ec4\uff08Associative Arrays\uff09\uff1a<\/strong> \u4f7f\u7528\u5b57\u7b26\u4e32\u952e\u6765\u8bbf\u95ee\u6570\u7ec4\u5143\u7d20\u3002<\/li>\n<\/ul>\n\n\n\n<p>PHP \u652f\u6301\u591a\u7ef4\u6570\u7ec4\uff0c\u5176\u4e2d\u6bcf\u4e00\u5c42\u6570\u7ec4\u90fd\u53ef\u4ee5\u662f\u7d22\u5f15\u6570\u7ec4\u6216\u5173\u8054\u6570\u7ec4\uff0c\u6216\u8005\u5b83\u4eec\u7684\u7ec4\u5408\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>$array = array(1, 2, 3);\necho $array&#91;0];  \/\/ \u8f93\u51fa 1<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u4e8c\u7ef4\u6570\u7ec4\uff08\u77e9\u9635\uff09<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\n$array = array(\n    array(1, 2, 3),\n    array(4, 5, 6),\n    array(7, 8, 9)\n);\n\n\/\/ \u8bbf\u95ee\u4e8c\u7ef4\u6570\u7ec4\u7684\u5143\u7d20\necho $array&#91;1]&#91;2];  \/\/ \u8f93\u51fa 6<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u5173\u8054\u6570\u7ec4\u7684\u591a\u7ef4\u6570\u7ec4<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>$array = array(\n    \"first\" =&gt; array(\"apple\", \"banana\", \"cherry\"),\n    \"second\" =&gt; array(\"dog\", \"elephant\", \"fox\"),\n    \"third\" =&gt; array(\"guitar\", \"harp\", \"piano\")\n);\n\n\/\/ \u8bbf\u95ee\u5173\u8054\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\necho $array&#91;\"second\"]&#91;1];  \/\/ \u8f93\u51fa elephant<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2\ufe0f\u20e3 \u591a\u7ef4\u6570\u7ec4\u7684\u5e38\u89c1\u64cd\u4f5c<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.1 \u904d\u5386\u591a\u7ef4\u6570\u7ec4<\/strong><\/h3>\n\n\n\n<p>\u904d\u5386\u591a\u7ef4\u6570\u7ec4\u6709\u4e24\u79cd\u5e38\u89c1\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528 <code>foreach<\/code> \u5faa\u73af\uff1a<\/strong> <code>foreach<\/code> \u5faa\u73af\u9002\u7528\u4e8e\u591a\u7ef4\u6570\u7ec4\uff0c\u80fd\u591f\u5d4c\u5957\u5faa\u73af\u6765\u5904\u7406\u6bcf\u4e00\u5c42\u6570\u7ec4\u3002<\/li>\n\n\n\n<li><strong>\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\uff1a<\/strong> \u53ef\u4ee5\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u904d\u5386\u6570\u7ec4\uff0c\u4f46\u9700\u8981\u77e5\u9053\u6570\u7ec4\u7684\u5927\u5c0f\u3002<\/li>\n<\/ul>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u4f7f\u7528 <code>foreach<\/code> \u904d\u5386\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>$array = array(\n    array(1, 2, 3),\n    array(4, 5, 6),\n    array(7, 8, 9)\n);\n\nforeach ($array as $sub_array) {\n    foreach ($sub_array as $value) {\n        echo $value . \" \";\n    }\n    echo \"&lt;br&gt;\";\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1 2 3\n4 5 6\n7 8 9<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u4f7f\u7528 <code>for<\/code> \u5faa\u73af\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>$array = array(\n    array(1, 2, 3),\n    array(4, 5, 6),\n    array(7, 8, 9)\n);\n\nfor ($i = 0; $i &lt; count($array); $i++) {\n    for ($j = 0; $j &lt; count($array&#91;$i]); $j++) {\n        echo $array&#91;$i]&#91;$j] . \" \";\n    }\n    echo \"&lt;br&gt;\";\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.2 \u6dfb\u52a0\u5143\u7d20\u5230\u591a\u7ef4\u6570\u7ec4<\/strong><\/h3>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>[]<\/code> \u8fd0\u7b97\u7b26\u6216 <code>array_push()<\/code> \u51fd\u6570\u5411\u591a\u7ef4\u6570\u7ec4\u6dfb\u52a0\u5143\u7d20\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u4f7f\u7528 <code>[]<\/code> \u6dfb\u52a0\u5143\u7d20<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>$array = array(\n    array(1, 2),\n    array(3, 4)\n);\n\n$array&#91;] = array(5, 6);  \/\/ \u6dfb\u52a0\u65b0\u7684\u4e00\u7ef4\u6570\u7ec4\n\nprint_r($array);<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Array\n(\n    &#91;0] =&gt; Array\n        (\n            &#91;0] =&gt; 1\n            &#91;1] =&gt; 2\n        )\n\n    &#91;1] =&gt; Array\n        (\n            &#91;0] =&gt; 3\n            &#91;1] =&gt; 4\n        )\n\n    &#91;2] =&gt; Array\n        (\n            &#91;0] =&gt; 5\n            &#91;1] =&gt; 6\n        )\n)<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u4f7f\u7528 <code>array_push()<\/code> \u6dfb\u52a0\u5143\u7d20<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>$array = array(\n    array(1, 2),\n    array(3, 4)\n);\n\narray_push($array, array(5, 6));  \/\/ \u6dfb\u52a0\u65b0\u7684\u4e00\u7ef4\u6570\u7ec4\n\nprint_r($array);<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.3 \u4fee\u6539\u591a\u7ef4\u6570\u7ec4\u4e2d\u7684\u5143\u7d20<\/strong><\/h3>\n\n\n\n<p>\u8981\u4fee\u6539\u591a\u7ef4\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\uff0c\u4f60\u53ea\u9700\u4f7f\u7528\u9002\u5f53\u7684\u7d22\u5f15\u6765\u8bbf\u95ee\u5e76\u66f4\u65b0\u503c\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u4fee\u6539\u4e8c\u7ef4\u6570\u7ec4\u7684\u5143\u7d20<\/strong><\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>$array = array(\n    array(1, 2, 3),\n    array(4, 5, 6)\n);\n\n\/\/ \u4fee\u6539\u5143\u7d20\n$array&#91;0]&#91;2] = 10;  \/\/ \u5c06\u7b2c\u4e00\u4e2a\u5b50\u6570\u7ec4\u7684\u7b2c\u4e09\u4e2a\u5143\u7d20\u4fee\u6539\u4e3a 10\n\nprint_r($array);<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Array\n(\n    &#91;0] =&gt; Array\n        (\n            &#91;0] =&gt; 1\n            &#91;1] =&gt; 2\n            &#91;2] =&gt; 10\n        )\n\n    &#91;1] =&gt; Array\n        (\n            &#91;0] =&gt; 4\n            &#91;1] =&gt; 5\n            &#91;2] =&gt; 6\n        )\n)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3\ufe0f\u20e3 \u9ad8\u7ef4\u6570\u7ec4<\/strong><\/h2>\n\n\n\n<p>PHP \u8fd8\u652f\u6301\u66f4\u9ad8\u7ef4\u5ea6\u7684\u6570\u7ec4\uff0c\u5373\u4e09\u7ef4\u3001\u56db\u7ef4\u7b49\uff0c\u64cd\u4f5c\u65b9\u5f0f\u4e0e\u4e8c\u7ef4\u6570\u7ec4\u76f8\u540c\u3002\u4f60\u53ea\u9700\u7ee7\u7eed\u589e\u52a0\u6570\u7ec4\u7684\u5c42\u7ea7\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u4e09\u7ef4\u6570\u7ec4<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$array = array(\n    array(\n        array(1, 2),\n        array(3, 4)\n    ),\n    array(\n        array(5, 6),\n        array(7, 8)\n    )\n);\n\n\/\/ \u8bbf\u95ee\u4e09\u7ef4\u6570\u7ec4\u5143\u7d20\necho $array&#91;1]&#91;0]&#91;1];  \/\/ \u8f93\u51fa 6<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4\ufe0f\u20e3 \u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u5904\u7406\u591a\u7ef4\u6570\u7ec4<\/strong><\/h2>\n\n\n\n<p>PHP \u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u51fd\u6570\u6765\u7b80\u5316\u591a\u7ef4\u6570\u7ec4\u7684\u5904\u7406\uff0c\u4f8b\u5982\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>array_map()<\/code>\uff1a\u5e94\u7528\u56de\u8c03\u51fd\u6570\u5230\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u3002<\/li>\n\n\n\n<li><code>array_walk_recursive()<\/code>\uff1a\u9012\u5f52\u5730\u5e94\u7528\u56de\u8c03\u51fd\u6570\u5230\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u793a\u4f8b\uff1a\u4f7f\u7528 <code>array_map()<\/code> \u5904\u7406\u591a\u7ef4\u6570\u7ec4<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>$array = array(\n    array(1, 2),\n    array(3, 4)\n);\n\n\/\/ \u5bf9\u6bcf\u4e2a\u5143\u7d20\u4e58\u4ee5 2\n$new_array = array_map(function($sub_array) {\n    return array_map(function($item) {\n        return $item * 2;\n    }, $sub_array);\n}, $array);\n\nprint_r($new_array);<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Array\n(\n    &#91;0] =&gt; Array\n        (\n            &#91;0] =&gt; 2\n            &#91;1] =&gt; 4\n        )\n\n    &#91;1] =&gt; Array\n        (\n            &#91;0] =&gt; 6\n            &#91;1] =&gt; 8\n        )\n)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5\ufe0f\u20e3 \u63a8\u8350\u9605\u8bfb\u4e0e\u6587\u6863<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.php.net\/manual\/zh\/language.types.array.php\">PHP \u6570\u7ec4\u5b98\u65b9\u6587\u6863<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.php.net\/manual\/zh\/function.array-map.php\">PHP array_map() \u51fd\u6570<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.php.net\/manual\/zh\/function.array-walk-recursive.php\">PHP array_walk_recursive() \u51fd\u6570<\/a><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u901a\u8fc7\u591a\u7ef4\u6570\u7ec4\uff0cPHP \u53ef\u4ee5\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u9002\u7528\u4e8e\u591a\u5c42\u5d4c\u5957\u3001\u77e9\u9635\u3001\u8868\u683c\u7b49\u590d\u6742\u6570\u636e\u3002\u901a\u8fc7\u9002\u5f53\u7684\u904d\u5386\u3001\u4fee\u6539\u548c\u5185\u7f6e\u51fd\u6570\uff0c\u64cd\u4f5c\u591a\u7ef4\u6570\u7ec4\u53d8\u5f97\u975e\u5e38\u65b9\u4fbf\u3002\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u591a\u7ef4\u6570\u7ec4\u662f\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u672c\u8eab\u662f\u6570\u7ec4\u7684\u6570\u7ec4\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u6570\u7ec4\u7684\u503c\u53ef\u4ee5\u662f\u53e6\u4e00\u4e2a\u6570\u7ec4\uff0c\u56e0\u6b64\u53ef\u4ee5\u521b\u5efa\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3032,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80],"tags":[],"class_list":["post-3031","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3031","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3031"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3031\/revisions"}],"predecessor-version":[{"id":3033,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3031\/revisions\/3033"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3032"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}