{"id":1413,"date":"2022-06-24T22:51:27","date_gmt":"2022-06-24T14:51:27","guid":{"rendered":"http:\/\/www.tra56.com\/?p=1413"},"modified":"2022-06-24T22:51:27","modified_gmt":"2022-06-24T14:51:27","slug":"1413","status":"publish","type":"post","link":"https:\/\/www.tra56.com\/1413.html","title":{"rendered":"array_map\u4e0earray_walk\u7684\u7528\u6cd5\u4e0e\u533a\u522b\u8be6\u89e3"},"content":{"rendered":"
\u4e00\u3001\u80cc\u666f<\/p>\n
\u4eca\u5929\u770b\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u53d1\u73b0\u4ee3\u7801\u4e2d\u4f7f\u7528array_map\u6765\u7edf\u4e00\u53bb\u6389\u6570\u7ec4\u4e2d\u7684\u7a7a\u683c\uff0c\u6bd4\u8d77\u5faa\u73af\u6765\uff0c\u65b9\u4fbf\u4e86\u5f88\u591a\u3002\u7f51\u4e0a\u767e\u5ea6\u4e00\u4e0barray_map\uff0c\u7136\u540e\u53d1\u73b0\u4e86array_walk\u3002\u597d\u5427\uff0c\u8fd9\u4e24\u4e2a\u51fd\u6570\u867d\u7136\u5728\u67d0\u4e9b\u60c5\u5883\u4e0b\u6709\u5f02\u66f2\u540c\u5de5\u4e4b\u5999\uff0c\u4f46\u662f\u533a\u522b\u8fd8\u662f\u5f88\u5927\u7684\u3002<\/p>\n
<\/p>\n
\u4e8c\u3001\u5173\u4e8earray_map<\/p>\n
1\u3001\u6587\u6863<\/p>\n
<\/p>\n
array_map() \u51fd\u6570\u5c06\u7528\u6237\u81ea\u5b9a\u4e49\u51fd\u6570\u4f5c\u7528\u5230\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u503c\u4e0a\uff0c\u5e76\u8fd4\u56de\u7528\u6237\u81ea\u5b9a\u4e49\u51fd\u6570\u4f5c\u7528\u540e\u7684\u5e26\u6709\u65b0\u503c\u7684\u6570\u7ec4\u3002<\/p>\n
<\/p>\n
\u56de\u8c03\u51fd\u6570\u63a5\u53d7\u7684\u53c2\u6570\u6570\u76ee\u5e94\u8be5\u548c\u4f20\u9012\u7ed9 array_map() \u51fd\u6570\u7684\u6570\u7ec4\u6570\u76ee\u4e00\u81f4\u3002<\/p>\n
http:\/\/www.w3school.com.cn\/php\/func_array_map.asp<\/p>\n
<\/p>\n
\u5927\u5bb6\u770b\u4e00\u4e0b\u6587\u6863\uff0c\u5c31\u77e5\u9053\u5927\u6982\u7684\u7528\u6cd5\u4e86\u3002<\/p>\n
\/\/\u8fd9\u91cc\u53ef\u4ee5\u4f20\u9012\u591a\u4e2a\u6570\u7ec4\u4f5c\u4e3a\u53c2\u6570\uff0c\u4e0e\u4e4b\u5bf9\u5e94\u7684\uff0cmyfunction()\u51fd\u6570\u4e2d\u4e5f\u8981\u6709\u591a\u4e2a\u53c2\u6570\u3002\u662f\u4e00\u4e00\u5bf9\u5e94\u7684\u5173\u7cfb\r\narray_map(myfunction,array1,array2,array3...)<\/pre>\n2\u3001\u4f8b\u5b50<\/p>\n
public function test_array_map(Request $request)\r\n {\r\n $aa = array('aa', 'bb');\r\n $bb = array_map(function($item) {\r\n $item = $item . 'aa' ;\r\n return $item;\r\n }, $aa);\r\n var_dump($aa);\r\n echo'<br\/>';\r\n var_dump($bb);\r\n }\r\n\/\/\u7ed3\u679c\r\n array(2) { [0]=> string(2) "aa" [1]=> string(2) "bb" }\r\n array(2) { [0]=> string(4) "aaaa" [1]=> string(4) "bbaa" }<\/pre>\n\u6b64\u5904\u53ef\u4ee5\u770b\u5230\uff0c$bb\u662f\u6211\u4eec\u65b0\u8fd4\u56de\u7684\u6570\u7ec4\uff0c\u4e5f\u662f\u7ecf\u8fc7\u6211\u4eec\u81ea\u5df1\u5904\u7406\u8fc7\u7684\u6570\u7ec4\u3002\u6b64\u65f6\u662f\u4e0d\u4f1a\u5f71\u54cd\u5230\u539f\u6570\u7ec4\u7684\u3002<\/p>\n
<\/p>\n
\u4e09\u3001array_walk<\/p>\n
1\u3001\u6587\u6863<\/p>\n
<\/p>\n
array_walk() \u51fd\u6570\u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u7528\u7528\u6237\u81ea\u5b9a\u4e49\u51fd\u6570\u3002\u5728\u51fd\u6570\u4e2d\uff0c\u6570\u7ec4\u7684\u952e\u540d\u548c\u952e\u503c\u662f\u53c2\u6570\u3002<\/p>\n
http:\/\/www.w3school.com.cn\/php\/func_array_walk.asp<\/p>\n
<\/p>\n
\u7528\u6cd5\uff1aarray_walk(array,myfunction,userdata\u2026)<\/p>\n
<\/p>\n
\u8fd9\u91cc\u7684\u4f8b\u5b50\u770b\u6587\u6863\u5c31\u53ef\u4ee5\uff0c\u6bd4\u8f83\u6e05\u695a\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\u8be5\u51fd\u6570\u7684\u8fd4\u56de\u503c\u662fbool\u503c<\/p>\n
<\/p>\n
2\u3001\u4f8b\u5b50\uff1a<\/p>\n
public function test_array_walk(Request $request)\r\n {\r\n $aa = array('aa', 'bb');\r\n $bb = array_walk($aa,function($item) {\r\n $item = $item . 'aa' ;\r\n return $item;\r\n });\r\n var_dump($aa);\r\n echo'<br\/>';\r\n var_dump($bb);\r\n }\r\n \/\/\u7ed3\u679c\r\n array(2) { [0]=> string(2) "aa" [1]=> string(2) "bb" }\r\n\t bool(true)<\/pre>\n\u8fd9\u91cc\u53d1\u73b0\u8fd4\u56de\u7684\u503c\u662fbool\u503c\u3002\u5982\u679c\u60f3\u8981\u8f93\u51fa\u7684\u8bdd\uff0c\u5c31\u5728\u51fd\u6570\u4e2d\u4f7f\u7528<\/span>
echo $item<\/code>\u3002\u8fd9\u91cc\u6253\u5370\u539f\u6570\u7ec4\u7684\u503c\u662f\u4e0d\u53d8\u7684\uff0c\u5982\u679c\u8981\u6539\u53d8\u539f\u6570\u7ec4\u7684\u503c\uff0c\u5219\u9700\u8981\u4f7f\u7528\u5f15\u7528\u7b26\u53f7\uff1a<\/span><\/p>\n
\/\/\u8fd9\u91cc\u52a0\u4e86\u4e2a\u5f15\u7528\r\n $bb = array_walk($aa,function(&$item) {\r\n $item = $item . 'aa' ;\r\n return $item;\r\n });\r\n \/\/\u5176\u4ed6\u4e0d\u53d8\u6253\u5370\u7ed3\u679c\uff08\u539f\u6570\u7ec4\u5df2\u7ecf\u88ab\u6539\u53d8\uff09\r\n array(2) { [0]=> string(4) "aaaa" [1]=> string(4) "bbaa" }\r\n bool(true)<\/pre>\n\u56db\u3001\u533a\u522b<\/h2>\n
1\u3001array_map\u7684\u7528\u6cd5\u662farray_map(\u51fd\u6570\u540d\uff0c\u6570\u7ec4)\uff0c\u800carray_walk\u7684\u7528\u6cd5\u662farray_walk(\u6570\u7ec4\uff0c\u51fd\u6570\u540d);\r\n\r\n2\u3001array_map\u91cc\u9762\u7684\u51fd\u6570\u53ef\u4ee5\u662f\u81ea\u5b9a\u4e49\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u662fphp\u81ea\u5e26\u7684\u51fd\u6570\uff0c\u6bd4\u5982trim\u53bb\u9664\u7a7a\u683c\u7b49\u3002\u800carray_walk\u91cc\u9762\u7684\u51fd\u6570\u53ea\u80fd\u662f\u81ea\u5b9a\u4e49\u7684\u51fd\u6570\r\n\r\n3\u3001array_map\u4e0d\u53ef\u4ee5\u6539\u53d8\u539f\u51fd\u6570\u7684\u503c\uff0c\u4f1a\u83b7\u53d6\u5230\u65b0\u7684\u6570\u7ec4\u3002array_walk\u662f\u53ef\u4ee5\u6539\u53d8\u539f\u51fd\u6570\u7684\u503c\u7684\uff08\u52a0\u4e2a\u5f15\u7528\uff09\u3002\r\n\r\n4\u3001array_map\u5fc5\u987b\u8981\u6709\u8fd4\u56de\u503c\uff0c\u56e0\u4e3a\u8981\u586b\u5145\u6570\u7ec4\u3002\u800carray_walk\u53ef\u4ee5\u6ca1\u6709\u8fd4\u56de\u503c\uff0c\u8f93\u51fa\u7684\u8bdd\u8981\u5728\u8c03\u7528\u7684\u51fd\u6570\u4e2d\u901a\u8fc7echo\u6765\u8f93\u51fa\u3002<\/pre>\n\u8fd9\u4e24\u4e2a\u51fd\u6570\u6709\u4e2a\u5171\u540c\u70b9\u5c31\u662f\u53ef\u4ee5\u7b80\u5316\u6211\u4eec\u7684\u4ee3\u7801\uff0c\u6709\u4e9b\u7b80\u5355\u7684foreach\u5faa\u73af\u66ff\u6362\u529f\u80fd\uff0c\u5b8c\u5168\u53ef\u4ee5\u7528\u8fd9\u4e24\u4e2a\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u8bb0\u5f55\u4e00\u4e0b\u3002<\/p>\n
end<\/span><\/p>\n
<\/p>\n","protected":false},"excerpt":{"rendered":"
\u4e00\u3001\u80cc\u666f \u4eca\u5929\u770b\u4ee3\u7801\u7684\u65f6\u5019\uff0c\u53d1\u73b0\u4ee3\u7801\u4e2d\u4f7f\u7528array_map\u6765\u7edf\u4e00\u53bb […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,92],"tags":[],"class_list":["post-1413","post","type-post","status-publish","format-standard","hentry","category-php","category-phpcode"],"_links":{"self":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/1413"}],"collection":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/comments?post=1413"}],"version-history":[{"count":1,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/1413\/revisions"}],"predecessor-version":[{"id":1414,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/1413\/revisions\/1414"}],"wp:attachment":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/media?parent=1413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/categories?post=1413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/tags?post=1413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}