{"id":217753,"date":"2023-12-22T23:41:29","date_gmt":"2023-12-22T15:41:29","guid":{"rendered":"https:\/\/www.idc.net\/help\/217753\/"},"modified":"2023-12-22T23:41:29","modified_gmt":"2023-12-22T15:41:29","slug":"php%e4%bd%bf%e7%94%a8%e6%89%8b%e5%86%8c%ef%bc%9a%e4%bd%bf%e7%94%a8array_map%e5%b0%86%e5%9b%9e%e8%b0%83%e5%87%bd%e6%95%b0%e5%ba%94%e7%94%a8%e5%88%b0%e7%bb%99%e5%ae%9a%e6%95%b0%e7%bb%84%e7%9a%84-2","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/217753\/","title":{"rendered":"PHP\u4f7f\u7528\u624b\u518c\uff1a\u4f7f\u7528array_map\u5c06\u56de\u8c03\u51fd\u6570\u5e94\u7528\u5230\u7ed9\u5b9a\u6570\u7ec4\u7684\u5143\u7d20\u4e0a"},"content":{"rendered":"<p><title id=\"php%e4%bd%bf%e7%94%a8%e6%89%8b%e5%86%8c%ef%bc%9a%e4%bd%bf%e7%94%a8array_map%e5%b0%86%e5%9b%9e%e8%b0%83%e5%87%bd%e6%95%b0%e5%ba%94%e7%94%a8%e5%88%b0%e7%bb%99%e5%ae%9a%e6%95%b0%e7%bb%84%e7%9a%84-PrkkpnKacB\">PHP\u4f7f\u7528\u624b\u518c\uff1a\u4f7f\u7528array_map\u5c06\u56de\u8c03\u51fd\u6570\u5e94\u7528\u5230\u7ed9\u5b9a\u6570\u7ec4\u7684\u5143\u7d20\u4e0a<\/title><\/p>\n<h1 id=\"php%e4%bd%bf%e7%94%a8%e6%89%8b%e5%86%8c%ef%bc%9a%e4%bd%bf%e7%94%a8array_map%e5%b0%86%e5%9b%9e%e8%b0%83%e5%87%bd%e6%95%b0%e5%ba%94%e7%94%a8%e5%88%b0%e7%bb%99%e5%ae%9a%e6%95%b0%e7%bb%84%e7%9a%84-PrkkpnKacB\">PHP\u4f7f\u7528\u624b\u518c\uff1a\u4f7f\u7528array_map\u5c06\u56de\u8c03\u51fd\u6570\u5e94\u7528\u5230\u7ed9\u5b9a\u6570\u7ec4\u7684\u5143\u7d20\u4e0a<\/h1>\n<p>\u5728PHP\u4e2d\uff0carray_map\u51fd\u6570\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u5c06\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u5e94\u7528\u5230\u7ed9\u5b9a\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u4e0a\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\uff0c\u65b0\u6570\u7ec4\u7684\u5143\u7d20\u662f\u539f\u6570\u7ec4\u5143\u7d20\u7ecf\u8fc7\u56de\u8c03\u51fd\u6570\u5904\u7406\u540e\u7684\u7ed3\u679c\u3002<\/p>\n<h2 id=\"array_map%e5%87%bd%e6%95%b0%e7%9a%84%e8%af%ad%e6%b3%95-PrkkpnKacB\">array_map\u51fd\u6570\u7684\u8bed\u6cd5<\/h2>\n<p>array_map\u51fd\u6570\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>\narray array_map ( callable $callback , array $array1 [, array $... ] )\n<\/pre>\n<p>\u5176\u4e2d\uff0c$callback\u662f\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u53ef\u4ee5\u662f\u4e00\u4e2a\u51fd\u6570\u540d\u7684\u5b57\u7b26\u4e32\uff0c\u4e5f\u53ef\u4ee5\u662f\u4e00\u4e2a\u533f\u540d\u51fd\u6570\u3002$array1\u662f\u8981\u5904\u7406\u7684\u6570\u7ec4\uff0c\u53ef\u4ee5\u6709\u591a\u4e2a\u6570\u7ec4\u4f5c\u4e3a\u53c2\u6570\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8array_map%e5%87%bd%e6%95%b0%e7%9a%84%e7%a4%ba%e4%be%8b-PrkkpnKacB\">\u4f7f\u7528array_map\u51fd\u6570\u7684\u793a\u4f8b<\/h2>\n<p>\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528array_map\u51fd\u6570\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>\n&lt;?php\nfunction square($n) {\n    return $n * $n;\n}\n\n$numbers = [1, 2, 3, 4, 5];\n$squaredNumbers = array_map(\"square\", $numbers);\n\nprint_r($squaredNumbers);\n?&gt;\n<\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3asquare\u7684\u51fd\u6570\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\u5e76\u8fd4\u56de\u8be5\u53c2\u6570\u7684\u5e73\u65b9\u3002\u7136\u540e\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6570\u7ec4$numbers\uff0c\u5305\u542b\u4e86\u4e00\u4e9b\u6570\u5b57\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u4f7f\u7528array_map\u51fd\u6570\u5c06square\u51fd\u6570\u5e94\u7528\u5230$numbers\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u4e0a\uff0c\u5f97\u5230\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4$squaredNumbers\uff0c\u65b0\u6570\u7ec4\u7684\u5143\u7d20\u662f\u539f\u6570\u7ec4\u5143\u7d20\u7684\u5e73\u65b9\u3002<\/p>\n<p>\u8fd0\u884c\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre>\nArray\n(\n    [0] =&gt; 1\n    [1] =&gt; 4\n    [2] =&gt; 9\n    [3] =&gt; 16\n    [4] =&gt; 25\n)\n<\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c$squaredNumbers\u6570\u7ec4\u7684\u5143\u7d20\u5206\u522b\u662f1\u7684\u5e73\u65b9\u30012\u7684\u5e73\u65b9\u30013\u7684\u5e73\u65b9\u30014\u7684\u5e73\u65b9\u548c5\u7684\u5e73\u65b9\u3002<\/p>\n<h2 id=\"%e4%bd%bf%e7%94%a8%e5%8c%bf%e5%90%8d%e5%87%bd%e6%95%b0%e4%bd%9c%e4%b8%ba%e5%9b%9e%e8%b0%83%e5%87%bd%e6%95%b0-PrkkpnKacB\">\u4f7f\u7528\u533f\u540d\u51fd\u6570\u4f5c\u4e3a\u56de\u8c03\u51fd\u6570<\/h2>\n<p>\u9664\u4e86\u4f7f\u7528\u51fd\u6570\u540d\u7684\u5b57\u7b26\u4e32\u4f5c\u4e3a\u56de\u8c03\u51fd\u6570\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u4f7f\u7528\u533f\u540d\u51fd\u6570\u4f5c\u4e3a\u56de\u8c03\u51fd\u6570\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u533f\u540d\u51fd\u6570\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>\n&lt;?php\n$numbers = [1, 2, 3, 4, 5];\n$multipliedNumbers = array_map(function($n) {\n    return $n * 2;\n}, $numbers);\n\nprint_r($multipliedNumbers);\n?&gt;\n<\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u4f7f\u7528\u4e86\u4e00\u4e2a\u533f\u540d\u51fd\u6570\u4f5c\u4e3a\u56de\u8c03\u51fd\u6570\uff0c\u8be5\u533f\u540d\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u53c2\u6570\u5e76\u8fd4\u56de\u8be5\u53c2\u6570\u7684\u4e24\u500d\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528array_map\u51fd\u6570\u5c06\u533f\u540d\u51fd\u6570\u5e94\u7528\u5230$numbers\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u4e0a\uff0c\u5f97\u5230\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4$multipliedNumbers\uff0c\u65b0\u6570\u7ec4\u7684\u5143\u7d20\u662f\u539f\u6570\u7ec4\u5143\u7d20\u7684\u4e24\u500d\u3002<\/p>\n<p>\u8fd0\u884c\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u8f93\u51fa\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n<pre>\nArray\n(\n    [0] =&gt; 2\n    [1] =&gt; 4\n    [2] =&gt; 6\n    [3] =&gt; 8\n    [4] =&gt; 10\n)\n<\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c$multipliedNumbers\u6570\u7ec4\u7684\u5143\u7d20\u5206\u522b\u662f\u539f\u6570\u7ec4\u5143\u7d20\u7684\u4e24\u500d\u3002<\/p>\n<h2 id=\"%e6%80%bb%e7%bb%93-PrkkpnKacB\">\u603b\u7ed3<\/h2>\n<p>array_map\u51fd\u6570\u662fPHP\u4e2d\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u51fd\u6570\uff0c\u5b83\u53ef\u4ee5\u5c06\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u5e94\u7528\u5230\u7ed9\u5b9a\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u4e0a\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570\u540d\u7684\u5b57\u7b26\u4e32\u6216\u533f\u540d\u51fd\u6570\u4f5c\u4e3a\u56de\u8c03\u51fd\u6570\uff0c\u6839\u636e\u9700\u8981\u5bf9\u6570\u7ec4\u7684\u5143\u7d20\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<p>\u9999\u6e2f\u670d\u52a1\u5668\u9996\u9009\u540e\u6d6a\u4e91\uff0c\u63d0\u4f9b\u7a33\u5b9a\u53ef\u9760\u7684\u9999\u6e2f\u670d\u52a1\u5668\u670d\u52a1\u3002\u60a8\u53ef\u4ee5\u8bbf\u95ee<a href=\"https:\/\/idc.net\">https:\/\/idc.net<\/a>\u4e86\u89e3\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u4f7f\u7528\u624b\u518c\uff1a\u4f7f\u7528array_map\u5c06\u56de\u8c03\u51fd\u6570\u5e94\u7528\u5230\u7ed9\u5b9a\u6570\u7ec4\u7684\u5143\u7d20\u4e0a PHP\u4f7f\u7528\u624b\u518c\uff1a\u4f7f\u7528array_map [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[197851],"tags":[],"class_list":["post-217753","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/217753","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=217753"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/217753\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=217753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=217753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=217753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}