{"id":47990,"date":"2024-12-02T11:56:11","date_gmt":"2024-12-02T03:56:11","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47990\/"},"modified":"2024-12-02T11:56:11","modified_gmt":"2024-12-02T03:56:11","slug":"php-%e5%87%bd%e6%95%b0%e6%95%b0%e7%bb%84%e6%93%8d%e4%bd%9c%ef%bc%9a%e6%8e%8c%e6%8f%a1%e9%ab%98%e7%ba%a7%e6%8a%80%e5%b7%a7","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47990\/","title":{"rendered":"PHP \u51fd\u6570\u6570\u7ec4\u64cd\u4f5c\uff1a\u638c\u63e1\u9ad8\u7ea7\u6280\u5de7"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u6570\u7ec4\u64cd\u4f5c\uff1a\u638c\u63e1\u9ad8\u7ea7\u6280\u5de7<\/h1>\n<p>\u4f60\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u51fd\u6570\u6570\u7ec4\u64cd\u4f5c\uff1a\u638c\u63e1\u9ad8\u7ea7\u6280\u5de7\u300b<\/span>\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01<\/p>\n<p>\u9ad8\u7ea7 PHP \u6570\u7ec4\u64cd\u4f5c\u51fd\u6570\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u5305\u62ec\uff1aarray_filter()\uff1a\u8fc7\u6ee4\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\u3002array_map()\uff1a\u5e94\u7528\u56de\u8c03\u51fd\u6570\u5e76\u521b\u5efa\u65b0\u6570\u7ec4\u3002array_reduce()\uff1a\u7d2f\u79ef\u64cd\u4f5c\u5e76\u8fd4\u56de\u5355\u4e00\u503c\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729947631671ce7efa46f5.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u6570\u7ec4\u64cd\u4f5c\uff1a\u638c\u63e1\u9ad8\u7ea7\u6280\u5de7\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u6570\u7ec4\u64cd\u4f5c\uff1a\u638c\u63e1\u9ad8\u7ea7\u6280\u5de7\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u6570\u7ec4\u64cd\u4f5c\uff1a\u638c\u63e1\u9ad8\u7ea7\u6280\u5de7<\/strong><\/p>\n<p>\u5728 PHP \u5f00\u53d1\u4e2d\uff0c\u6570\u7ec4\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\uff0c\u5b83\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u5b58\u50a8\u548c\u7ba1\u7406\u6570\u636e\u7684\u6709\u5e8f\u65b9\u6cd5\u3002PHP \u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u51fd\u6570\u6765\u5904\u7406\u6570\u7ec4\uff0c\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u7b80\u5316\u6211\u4eec\u7684\u7f16\u7801\u4efb\u52a1\u3002\u672c\u6587\u5c06\u6df1\u5165\u5256\u6790 PHP \u4e2d\u4e00\u4e9b\u9ad8\u7ea7\u6570\u7ec4\u64cd\u4f5c\u51fd\u6570\uff0c\u5e76\u901a\u8fc7\u5b9e\u9645\u793a\u4f8b\u5c55\u793a\u5176\u7528\u6cd5\u3002<\/p>\n<p><strong>array_filter()<\/strong><\/p>\n<p><code>array_filter()<\/code> \u51fd\u6570\u7528\u4e8e\u4ece\u6570\u7ec4\u4e2d\u8fc7\u6ee4\u51fa\u7b26\u5408\u6307\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u3002\u5176\u8bed\u6cd5\u4e3a\uff1a<\/p>\n<pre>array_filter(array $array, callable $callback)<\/pre>\n<p>\u5176\u4e2d\uff0c<code>$callback<\/code> \u662f\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u5b83\u5c06\u63a5\u6536\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\u3002\u5982\u679c\u56de\u8c03\u51fd\u6570\u8fd4\u56de <code>true<\/code>\uff0c\u5219\u8be5\u5143\u7d20\u5c06\u4fdd\u7559\u5728\u8fc7\u6ee4\u540e\u7684\u6570\u7ec4\u4e2d\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u8fc7\u6ee4\u4e00\u4e2a\u6570\u5b57\u6570\u7ec4\uff0c\u53ea\u7559\u4e0b\u5927\u4e8e 10 \u7684\u5143\u7d20\uff1a<\/p>\n<pre>$numbers = [0, 1, 5, 11, 12, 15];\n\n$greaterThan10 = array_filter($numbers, function($num) {\n    return $num &gt; 10;\n});\n\nprint_r($greaterThan10); \/\/ [11, 12, 15]<\/pre>\n<p><strong>array_map()<\/strong><\/p>\n<p><code>array_map()<\/code> \u51fd\u6570\u5c06\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u5e94\u7528\u5230\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u6570\u7ec4\u3002\u5176\u8bed\u6cd5\u4e3a\uff1a<\/p>\n<pre>array_map(callable $callback, array $array)<\/pre>\n<p>\u56de\u8c03\u51fd\u6570\u5c06\u63a5\u6536\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u503c\u3002\u8fd4\u56de\u7684\u65b0\u6570\u7ec4\u5c06\u5305\u542b\u7531\u56de\u8c03\u51fd\u6570\u751f\u6210\u7684\u503c\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u5c06\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4\u8f6c\u6362\u4e3a\u5927\u5199\uff1a<\/p>\n<pre>$strings = ['apple', 'banana', 'cherry'];\n\n$uppercased = array_map('strtoupper', $strings);\n\nprint_r($uppercased); \/\/ ['APPLE', 'BANANA', 'CHERRY']<\/pre>\n<p><strong>array_reduce()<\/strong><\/p>\n<p><code>array_reduce()<\/code> \u51fd\u6570\u5bf9\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u7d2f\u79ef\u64cd\u4f5c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5355\u4e00\u503c\u3002\u5176\u8bed\u6cd5\u4e3a\uff1a<\/p>\n<pre>array_reduce(array $array, callable $callback, mixed $initial)<\/pre>\n<p>\u56de\u8c03\u51fd\u6570\u5c06\u63a5\u6536\u4e24\u4e2a\u53c2\u6570\uff1a\u7d2f\u79ef\u5668\uff08<code>$initial<\/code> \u53c2\u6570\u6216\u4e0a\u4e00\u6b21\u56de\u8c03\u8c03\u7528\u7684\u7ed3\u679c\uff09\u548c\u5f53\u524d\u5143\u7d20\u3002\u5b83\u5c06\u8fd4\u56de\u4e00\u4e2a\u65b0\u503c\uff0c\u8be5\u503c\u5c06\u7528\u4f5c\u4e0b\u4e00\u6b21\u56de\u8c03\u8c03\u7528\u7684\u7d2f\u79ef\u5668\u3002<code>$initial<\/code> \u53c2\u6570\u6307\u5b9a\u7d2f\u79ef\u64cd\u4f5c\u7684\u521d\u59cb\u503c\uff08\u53ef\u9009\uff09\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<p>\u8ba1\u7b97\u4e00\u4e2a\u6570\u5b57\u6570\u7ec4\u7684\u548c\uff1a<\/p>\n<pre>$numbers = [1, 2, 3, 4, 5];\n\n$sum = array_reduce($numbers, function($accumulator, $num) {\n    return $accumulator + $num;\n}, 0); \/\/ \u521d\u59cb\u503c\u8bbe\u7f6e\u4e3a 0\n\necho $sum; \/\/ 15<\/pre>\n<p><strong>\u603b\u7ed3<\/strong><\/p>\n<p>\uff08\u6b64\u5904\u4e0d\u4f1a\u6dfb\u52a0\u603b\u7ed3\u8bed\uff09<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aPHP \u51fd\u6570\u6570\u7ec4\u64cd\u4f5c\uff1a\u638c\u63e1\u9ad8\u7ea7\u6280\u5de7\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u6570\u7ec4\u64cd\u4f5c\uff1a\u638c\u63e1\u9ad8\u7ea7\u6280\u5de7 \u4f60\u5728\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u7684\u77e5\u8bc6\u5417\uff1f\u672c\u6587\u300aPHP \u51fd\u6570\u6570\u7ec4\u64cd\u4f5c\uff1a\u638c\u63e1\u9ad8\u7ea7\u6280\u5de7\u300b\uff0c\u4e3b\u8981\u4ecb\u7ecd\u7684\u5185\u5bb9\u5c31\u6d89\u53ca\u5230\uff0c\u5982\u679c\u4f60\u60f3\u63d0\u5347\u81ea\u5df1\u7684\u5f00\u53d1\u80fd\u529b\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0\uff0c\u5927\u5bb6\u8981\u77e5\u9053\u7f16\u7a0b\u7406\u8bba\u57fa\u7840\u548c\u5b9e\u6218\u64cd\u4f5c\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u54e6\uff01 \u9ad8\u7ea7 PHP \u6570\u7ec4\u64cd\u4f5c\u51fd\u6570\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u5305\u62ec\uff1aarray_filter()\uff1a\u8fc7\u6ee4\u7b26\u5408\u6761\u4ef6\u7684\u5143\u7d20\u3002array_map()\uff1a\u5e94\u7528\u56de\u8c03\u51fd\u6570\u5e76\u521b\u5efa\u65b0\u6570\u7ec4\u3002array_reduce()\uff1a\u7d2f\u79ef\u64cd\u4f5c\u5e76\u8fd4\u56de\u5355\u4e00\u503c\u3002 PHP \u51fd\u6570\u6570\u7ec4\u64cd\u4f5c\uff1a\u638c\u63e1\u9ad8\u7ea7\u6280\u5de7 \u5728 PHP \u5f00\u53d1\u4e2d\uff0c\u6570\u7ec4\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\uff0c\u5b83\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u5b58\u50a8\u548c\u7ba1\u7406\u6570\u636e\u7684\u6709\u5e8f\u65b9\u6cd5\u3002PHP \u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u51fd\u6570\u6765\u5904\u7406\u6570\u7ec4\uff0c\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u7b80\u5316\u6211\u4eec\u7684\u7f16\u7801\u4efb\u52a1\u3002\u672c\u6587\u5c06\u6df1\u5165\u5256\u6790 PHP \u4e2d\u4e00\u4e9b\u9ad8\u7ea7\u6570\u7ec4\u64cd\u4f5c\u51fd\u6570\uff0c\u5e76\u901a\u8fc7\u5b9e\u9645\u793a\u4f8b\u5c55\u793a\u5176\u7528\u6cd5\u3002 array_filter() array_filter() \u51fd\u6570\u7528\u4e8e\u4ece\u6570\u7ec4\u4e2d\u8fc7\u6ee4\u51fa\u7b26\u5408\u6307\u5b9a\u6761\u4ef6\u7684\u5143\u7d20\u3002\u5176\u8bed\u6cd5\u4e3a\uff1a array_filter(array $array, callable $callback) \u5176\u4e2d\uff0c$callback \u662f\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\uff0c\u5b83\u5c06\u63a5\u6536\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a\u5e03\u5c14\u503c\u3002\u5982\u679c\u56de\u8c03\u51fd\u6570\u8fd4\u56de true\uff0c\u5219\u8be5\u5143\u7d20\u5c06\u4fdd\u7559\u5728\u8fc7\u6ee4\u540e\u7684\u6570\u7ec4\u4e2d\u3002 \u793a\u4f8b\uff1a \u8fc7\u6ee4\u4e00\u4e2a\u6570\u5b57\u6570\u7ec4\uff0c\u53ea\u7559\u4e0b\u5927\u4e8e 10 \u7684\u5143\u7d20\uff1a $numbers = [0, 1, 5, 11, 12, 15]; $greaterThan10 = array_filter($numbers, function($num) { return $num &gt; 10; }); print_r($greaterThan10); \/\/ [11, 12, 15] array_map() array_map() \u51fd\u6570\u5c06\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u5e94\u7528\u5230\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u6570\u7ec4\u3002\u5176\u8bed\u6cd5\u4e3a\uff1a [&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-47990","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47990","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=47990"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47990\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47990"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}