{"id":181191,"date":"2024-05-08T20:51:56","date_gmt":"2024-05-08T12:51:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/181191.html"},"modified":"2024-05-08T20:52:01","modified_gmt":"2024-05-08T12:52:01","slug":"php-%e4%ba%8c%e7%bb%b4%e6%95%b0%e7%bb%84%e5%a6%82%e4%bd%95%e8%b5%8b%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/181191.html","title":{"rendered":"PHP \u4e8c\u7ef4\u6570\u7ec4\u5982\u4f55\u8d4b\u503c"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27070748\/98a32646-9add-483b-bf29-cbaca466c75c.webp\" alt=\"PHP \u4e8c\u7ef4\u6570\u7ec4\u5982\u4f55\u8d4b\u503c\" \/><\/p>\n<p><p>PHP \u4e2d\u4e8c\u7ef4\u6570\u7ec4\u8d4b\u503c\u7684\u65b9\u5f0f\u4e3b\u8981\u6709\u4e09\u79cd\uff1a<strong>\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528\u5faa\u73af\u3001\u901a\u8fc7\u6570\u7ec4\u51fd\u6570<\/strong>\u3002\u5176\u4e2d\uff0c<strong>\u76f4\u63a5\u8d4b\u503c<\/strong>\u662f\u6700\u57fa\u672c\u4e14\u76f4\u63a5\u7684\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u5df2\u77e5\u6570\u7ec4\u7684\u5177\u4f53\u5185\u5bb9\u65f6\u3002\u5982\u6b64\uff0c\u4f60\u53ef\u4ee5\u76f4\u63a5\u6307\u5b9a\u6bcf\u4e2a\u5143\u7d20\u7684\u503c\uff0c\u8fd9\u79cd\u65b9\u6cd5\u5bf9\u4e8e\u5c0f\u578b\u6216\u7b80\u5355\u7684\u6570\u7ec4\u6765\u8bf4\u975e\u5e38\u9ad8\u6548\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u4e86\u89e3\u5982\u4f55\u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u3002\u5047\u8bbe\u4f60\u8981\u521b\u5efa\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u6765\u5b58\u50a8\u5b66\u751f\u7684\u4fe1\u606f\uff0c\u5305\u62ec\u59d3\u540d\u548c\u5206\u6570\u3002\u5176\u6b21\uff0c\u901a\u8fc7\u76f4\u63a5\u8d4b\u503c\u7684\u65b9\u5f0f\uff0c\u4f60\u53ef\u4ee5\u521d\u59cb\u5316\u8fd9\u4e2a\u6570\u7ec4\uff0c\u5e76\u7acb\u5373\u7ed9\u5b83\u8d4b\u503c\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982:<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">$students = array(<\/p>\n<p>    array(&quot;John&quot;, 88),<\/p>\n<p>    array(&quot;Sara&quot;, 91),<\/p>\n<p>    array(&quot;Lily&quot;, 84)<\/p>\n<p>);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>$students<\/code> \u662f\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\uff0c\u6bcf\u4e00\u884c\u4ee3\u8868\u4e00\u4e2a\u5b66\u751f\u7684\u4fe1\u606f\uff0c\u5176\u4e2d\u5305\u62ec\u5b66\u751f\u7684\u59d3\u540d\u548c\u5206\u6570\u3002\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u76f4\u89c2\uff0c\u9002\u7528\u4e8e\u6570\u7ec4\u5927\u5c0f\u548c\u5185\u5bb9\u4e8b\u5148\u5df2\u77e5\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u76f4\u63a5\u8d4b\u503c<\/h2>\n<\/p>\n<p><h3>\u521d\u59cb\u5316\u548c\u8d4b\u503c\u65b9\u5f0f<\/h3>\n<\/p>\n<p><p>\u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4\u5e76\u76f4\u63a5\u8d4b\u503c\u662f\u6700\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u5f0f\u3002\u4f60\u53ef\u4ee5\u5728\u58f0\u660e\u6570\u7ec4\u65f6\u5c31\u6307\u5b9a\u5176\u4e2d\u7684\u5143\u7d20\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u6570\u7ec4\u7684\u7ed3\u6784\u548c\u5185\u5bb9\u90fd\u76f8\u5bf9\u56fa\u5b9a\u7684\u60c5\u51b5\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u5b66\u751f\u4fe1\u606f\uff08\u6bcf\u4e2a\u5b66\u751f\u7684\u59d3\u540d\u548c\u5206\u6570\uff09\u7684\u4e8c\u7ef4\u6570\u7ec4\uff0c\u5982\u4e0a\u793a\u4f8b\u6240\u793a\u3002<\/p>\n<\/p>\n<p><h3>\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u76f4\u63a5\u8d4b\u503c\u9002\u5408\u4e8e\u5904\u7406\u9759\u6001\u6570\u636e\uff0c\u6216\u8005\u5f53\u4f60\u7cbe\u786e\u77e5\u9053\u6570\u7ec4\u5c06\u4f1a\u5305\u542b\u54ea\u4e9b\u6570\u636e\u65f6\u4f7f\u7528\u3002\u5982\u679c\u6570\u7ec4\u7684\u5927\u5c0f\u548c\u5185\u5bb9\u4f1a\u9891\u7e41\u53d8\u52a8\uff0c\u53ef\u80fd\u9700\u8981\u8003\u8651\u5176\u4ed6\u8d4b\u503c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001\u4f7f\u7528\u5faa\u73af<\/h2>\n<\/p>\n<p><h3>\u52a8\u6001\u8d4b\u503c\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5f53\u4f60\u5904\u7406\u7684\u662f\u52a8\u6001\u6570\u636e\uff0c\u6216\u8005\u4e8c\u7ef4\u6570\u7ec4\u7684\u5927\u5c0f\u4e0d\u662f\u56fa\u5b9a\u7684\u65f6\uff0c\u4f7f\u7528\u5faa\u73af\u8fdb\u884c\u8d4b\u503c\u4f1a\u66f4\u52a0\u7075\u6d3b\u3002\u4f8b\u5982\uff0c\u5f53\u4f60\u4ece\u6570\u636e\u5e93\u8bfb\u53d6\u6570\u636e\u96c6\u65f6\uff0c\u5e76\u4e0d\u77e5\u9053\u4f1a\u6709\u591a\u5c11\u884c\u6570\u636e\u8fd4\u56de\u3002\u8fd9\u65f6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6570\u636e\u96c6\uff0c\u5e76\u52a8\u6001\u5730\u5c06\u6570\u636e\u8d4b\u503c\u7ed9\u4e8c\u7ef4\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">$students = array();<\/p>\n<p>while ($row = fetchStudentData()) { \/\/ \u5047\u8bbe fetchStudentData() \u662f\u4ece\u6570\u636e\u5e93\u83b7\u53d6\u6570\u636e\u7684\u51fd\u6570<\/p>\n<p>    $students[] = array($row[&#039;name&#039;], $row[&#039;score&#039;]);<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u52a8\u6001\u6570\u7ec4\u7684\u4f18\u52bf<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u5faa\u73af\u8d4b\u503c\u4e0d\u4ec5\u53ef\u4ee5\u5904\u7406\u4e0d\u786e\u5b9a\u6570\u91cf\u7684\u6570\u636e\uff0c\u800c\u4e14\u53ef\u4ee5\u7075\u6d3b\u8fd0\u7528\u4e8e\u5404\u79cd\u60c5\u51b5\uff0c\u5982\u4ece\u5916\u90e8\u8d44\u6e90\u5982\u6570\u636e\u5e93\u3001\u6587\u4ef6\u7b49\u52a8\u6001\u83b7\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528\u6570\u7ec4\u51fd\u6570<\/h2>\n<\/p>\n<p><h3>\u4f7f\u7528 array_map<\/h3>\n<\/p>\n<p><p>PHP \u4e2d\u7684 <code>array_map()<\/code> \u51fd\u6570\u53ef\u4ee5\u5e94\u7528\u4e8e\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\uff0c\u53ef\u4ee5\u7b80\u5316\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u7684\u64cd\u4f5c\uff0c\u7279\u522b\u662f\u5f53\u4f60\u9700\u8981\u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u8f6c\u6362\u65f6\u3002\u4f8b\u5982\uff0c\u5c06\u4e00\u4e2a\u4e00\u7ef4\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u8f6c\u6362\u6210\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-php\">$studentsDet<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ls = array(&#039;John:88&#039;, &#039;Sara:91&#039;, &#039;Lily:84&#039;);<\/p>\n<p>$students = array_map(function($item) {<\/p>\n<p>    return explode(&#039;:&#039;, $item);<\/p>\n<p>}, $studentsDetails);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6bcf\u4e2a\u5b66\u751f\u7684\u59d3\u540d\u548c\u5206\u6570\u4ee5\u201c\u59d3\u540d:\u5206\u6570\u201d\u7684\u5f62\u5f0f\u5b58\u50a8\u5728\u4e00\u7ef4\u6570\u7ec4 <code>$studentsDetails<\/code> \u4e2d\uff0c\u4f7f\u7528 <code>array_map<\/code> \u52a0\u4e0a\u533f\u540d\u51fd\u6570\u5206\u5272\u8fd9\u4e9b\u5b57\u7b26\u4e32\uff0c\u751f\u6210\u65b0\u7684\u4e8c\u7ef4\u6570\u7ec4 <code>$students<\/code>\u3002<\/p>\n<\/p>\n<p><h3>\u5229\u7528\u5176\u4ed6\u6570\u7ec4\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u9664\u4e86 <code>array_map()<\/code>\uff0cPHP \u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u5176\u4ed6\u5b9e\u7528\u7684\u6570\u7ec4\u51fd\u6570\uff0c\u5982 <code>array_reduce()<\/code>, <code>array_filter()<\/code> \u7b49\uff0c\u53ef\u4ee5\u7075\u6d3b\u4f7f\u7528\u5b83\u4eec\u5904\u7406\u590d\u6742\u7684\u6570\u7ec4\u64cd\u4f5c\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>PHP \u4e2d\u7684\u4e8c\u7ef4\u6570\u7ec4\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u7ec4\u7ec7\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u5904\u7406\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002\u901a\u8fc7<strong>\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528\u5faa\u73af\u3001\u4ee5\u53ca\u5229\u7528\u6570\u7ec4\u51fd\u6570<\/strong>\uff0c\u53ef\u4ee5\u7075\u6d3b\u9ad8\u6548\u5730\u5bf9\u4e8c\u7ef4\u6570\u7ec4\u8fdb\u884c\u64cd\u4f5c\u3002\u9009\u62e9\u6700\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4f60\u7684\u5177\u4f53\u9700\u6c42\uff0c\u4f8b\u5982\uff0c\u76f4\u63a5\u8d4b\u503c\u9002\u4e8e\u5904\u7406\u9759\u6001\u6570\u636e\uff0c\u5faa\u73af\u8d4b\u503c\u548c\u6570\u7ec4\u51fd\u6570\u9002\u4e8e\u5904\u7406\u52a8\u6001\u6216\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u3002\u7406\u89e3\u5e76\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5c06\u6709\u52a9\u4e8e\u4f60\u66f4\u9ad8\u6548\u5730\u5904\u7406\u548c\u7ec4\u7ec7\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u7ed9PHP\u4e8c\u7ef4\u6570\u7ec4\u8d4b\u503c\uff1f<\/strong><br \/>\n\u7ed9PHP\u4e8c\u7ef4\u6570\u7ec4\u8d4b\u503c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\uff1a<\/p>\n<ul>\n<li>\u904d\u5386\u8d4b\u503c\uff1a\u4f7f\u7528\u5faa\u73af\u7ed3\u6784\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u7136\u540e\u8fdb\u884c\u8d4b\u503c\u64cd\u4f5c\u3002<\/li>\n<li>\u76f4\u63a5\u8d4b\u503c\uff1a\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u7d22\u5f15\u7684\u65b9\u5f0f\u76f4\u63a5\u4e3a\u4e8c\u7ef4\u6570\u7ec4\u7684\u5143\u7d20\u8d4b\u503c\uff0c\u4f8b\u5982\uff1a$arr[0][0] = 1; $arr[0][1] = 2;<\/li>\n<li>\u6279\u91cf\u8d4b\u503c\uff1a\u53ef\u4ee5\u4f7f\u7528array()\u51fd\u6570\u5c06\u4e00\u7ec4\u503c\u8d4b\u7ed9\u4e8c\u7ef4\u6570\u7ec4\u7684\u67d0\u4e2a\u7ef4\u5ea6\uff0c\u4f8b\u5982\uff1a$arr[0] = array(1, 2, 3);<\/li>\n<\/ul>\n<p><strong>2. \u5982\u4f55\u7ed9PHP\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8d4b\u76f8\u540c\u7684\u503c\uff1f<\/strong><br \/>\n\u5982\u679c\u60f3\u7ed9\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8d4b\u76f8\u540c\u7684\u503c\uff0c\u53ef\u4ee5\u5229\u7528\u5d4c\u5957\u5faa\u73af\u904d\u5386\u4e8c\u7ef4\u6570\u7ec4\uff0c\u7136\u540e\u4e3a\u6bcf\u4e2a\u5143\u7d20\u8d4b\u76f8\u540c\u7684\u503c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-php\">for($i = 0; $i &lt; count($arr); $i++) {\n  for($j = 0; $j &lt; count($arr[$i]); $j++) {\n    $arr[$i][$j] = 10;\n  }\n}\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u5c06\u4f1a\u5c06\u4e8c\u7ef4\u6570\u7ec4$arr\u7684\u6bcf\u4e2a\u5143\u7d20\u8d4b\u503c\u4e3a10\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u5c06\u5176\u4ed6\u6570\u7ec4\u7684\u503c\u8d4b\u7ed9\u4e8c\u7ef4\u6570\u7ec4\uff1f<\/strong><br \/>\n\u5982\u679c\u60f3\u5c06\u5176\u4ed6\u6570\u7ec4\u7684\u503c\u8d4b\u7ed9\u4e8c\u7ef4\u6570\u7ec4\uff0c\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u904d\u5386\u5176\u4ed6\u6570\u7ec4\uff0c\u5e76\u5c06\u5bf9\u5e94\u7684\u503c\u8d4b\u7ed9\u4e8c\u7ef4\u6570\u7ec4\u7684\u5bf9\u5e94\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-php\">$otherArray = array(1, 2, 3, 4, 5);\nfor($i = 0; $i &lt; count($arr); $i++) {\n  for($j = 0; $j &lt; count($arr[$i]); $j++) {\n    $arr[$i][$j] = $otherArray[$i + $j];\n  }\n}\n<\/code><\/pre>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u5c06\u4f1a\u5c06$otherArray\u6570\u7ec4\u7684\u503c\u6309\u987a\u5e8f\u8d4b\u7ed9\u4e8c\u7ef4\u6570\u7ec4$arr\u7684\u6bcf\u4e2a\u5143\u7d20\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"PHP \u4e2d\u4e8c\u7ef4\u6570\u7ec4\u8d4b\u503c\u7684\u65b9\u5f0f\u4e3b\u8981\u6709\u4e09\u79cd\uff1a\u76f4\u63a5\u8d4b\u503c\u3001\u4f7f\u7528\u5faa\u73af\u3001\u901a\u8fc7\u6570\u7ec4\u51fd\u6570\u3002\u5176\u4e2d\uff0c\u76f4\u63a5\u8d4b\u503c\u662f\u6700\u57fa\u672c\u4e14\u76f4\u63a5\u7684\u65b9\u5f0f\uff0c [&hellip;]","protected":false},"author":3,"featured_media":181196,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/181191"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=181191"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/181191\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/181196"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=181191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=181191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=181191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}