{"id":202432,"date":"2025-05-13T11:20:33","date_gmt":"2025-05-13T03:20:33","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202432\/"},"modified":"2025-05-13T11:20:33","modified_gmt":"2025-05-13T03:20:33","slug":"php-%e5%87%bd%e6%95%b0%e5%bc%8f%e7%bc%96%e7%a8%8b%e6%8c%87%e5%8d%97%ef%bc%9a%e5%88%9d%e5%ad%a6%e8%80%85%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202432\/","title":{"rendered":"PHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u521d\u5b66\u8005\u6559\u7a0b"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u521d\u5b66\u8005\u6559\u7a0b<\/h1>\n<p>\u5c0f\u4f19\u4f34\u4eec\u6709\u6ca1\u6709\u89c9\u5f97\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u5f88\u6709\u610f\u601d\uff1f\u6709\u610f\u601d\u5c31\u5bf9\u4e86\uff01\u4eca\u5929\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u521d\u5b66\u8005\u6559\u7a0b\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u5728\u5b66\u4e60\u4e2d\u5bf9\u5176\u4e2d\u90e8\u5206\u77e5\u8bc6\u70b9\u6709\u7591\u95ee\uff0c\u6216\u8bb8\u770b\u4e86\u672c\u6587\u5c31\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<p>\u51fd\u6570\u5f0f\u7f16\u7a0b\u662f\u4e00\u79cd\u5f3a\u8c03\u7eaf\u51fd\u6570\u3001\u4e0d\u53ef\u53d8\u6570\u636e\u7ed3\u6784\u548c\u9012\u5f52\u7684\u7f16\u7a0b\u8303\u5f0f\u3002\u5b83\u4e0e\u5176\u4ed6\u7f16\u7a0b\u8303\u5f0f\u5f62\u6210\u9c9c\u660e\u5bf9\u6bd4\u3002PHP \u4e2d\u7684\u51fd\u6570\u5f0f\u7f16\u7a0b\u5185\u7f6e\u51fd\u6570\u652f\u6301\u51fd\u6570\u5f0f\u7f16\u7a0b\uff0c\u5305\u62ec\uff1aarray_map()\uff1a\u5c06\u51fd\u6570\u5e94\u7528\u4e8e\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002array_filter()\uff1a\u8fd4\u56de\u901a\u8fc7\u7ed9\u5b9a\u51fd\u6570\u6d4b\u8bd5\u7684\u6570\u7ec4\u5143\u7d20\u3002array_reduce()\uff1a\u5c06\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u7d2f\u79ef\u4e3a\u5355\u4e2a\u503c\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241015\/1728997309670e67bd10a78.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>PHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u521d\u5b66\u8005\u6559\u7a0b<\/strong><\/p>\n<p>\u51fd\u6570\u5f0f\u7f16\u7a0b\u662f\u4e00\u79cd\u7f16\u7a0b\u8303\u5f0f\uff0c\u5b83\u5f3a\u8c03\u4f7f\u7528\u7eaf\u51fd\u6570\u3001\u4e0d\u53ef\u53d8\u7684\u6570\u636e\u7ed3\u6784\u548c\u9012\u5f52\u3002\u5b83\u4e0e\u9762\u5411\u5bf9\u8c61\u7684\u7f16\u7a0b\u548c\u547d\u4ee4\u5f0f\u7f16\u7a0b\u5f62\u6210\u9c9c\u660e\u5bf9\u6bd4\uff0c\u540e\u8005\u66f4\u5f3a\u8c03\u5bf9\u8c61\u548c\u72b6\u6001\u3002<\/p>\n<p><strong>\u4ec0\u4e48\u662f\u7eaf\u51fd\u6570\uff1f<\/strong><\/p>\n<p>\u7eaf\u51fd\u6570\u662f\u4e0d\u4f9d\u8d56\u4e8e\u5916\u90e8\u72b6\u6001\u6216\u53ef\u53d8\u53d8\u91cf\u7684\u51fd\u6570\u3002\u8fd9\u610f\u5473\u7740\u5b83\u4eec\u603b\u662f\u5728\u7ed9\u5b9a\u76f8\u540c\u8f93\u5165\u65f6\u8fd4\u56de\u76f8\u540c\u8f93\u51fa\uff0c\u5e76\u4e14\u6ca1\u6709\u526f\u4f5c\u7528\uff08\u4f8b\u5982\u4fee\u6539\u5168\u5c40\u53d8\u91cf\uff09\u3002<\/p>\n<p><strong>\u4e0d\u53ef\u53d8\u7684\u6570\u636e\u7ed3\u6784<\/strong><\/p>\n<p>\u4e0d\u53ef\u53d8\u7684\u6570\u636e\u7ed3\u6784\u662f\u4e0d\u80fd\u88ab\u4fee\u6539\u7684\u3002\u8fd9\u610f\u5473\u7740\u4e00\u65e6\u521b\u5efa\u6570\u636e\u7ed3\u6784\uff0c\u5176\u503c\u5c31\u4e0d\u4f1a\u6539\u53d8\u3002\u8fd9\u4e0e\u53ef\u53d8\u6570\u636e\u7ed3\u6784\u76f8\u53cd\uff0c\u53ef\u53d8\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u901a\u8fc7\u5f15\u7528\u8fdb\u884c\u4fee\u6539\u3002<\/p>\n<p><strong>\u9012\u5f52<\/strong><\/p>\n<p>\u9012\u5f52\u662f\u4e00\u79cd\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u7684\u6280\u672f\u3002\u5b83\u901a\u5e38\u7528\u4e8e\u89e3\u51b3\u53ef\u4ee5\u5206\u89e3\u6210\u8f83\u5c0f\u5b9e\u4f8b\u7684\u95ee\u9898\u3002<\/p>\n<p><strong>PHP \u4e2d\u7684\u51fd\u6570\u5f0f\u7f16\u7a0b<\/strong><\/p>\n<p>PHP \u4e2d\u6709\u4e00\u4e9b\u5185\u7f6e\u51fd\u6570\u652f\u6301\u51fd\u6570\u5f0f\u7f16\u7a0b\u3002\u5176\u4e2d\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><strong>array_map()<\/strong>\uff1a\u5c06\u4e00\u4e2a\u51fd\u6570\u5e94\u7528\u4e8e\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002<\/li>\n<li><strong>array_filter()<\/strong>\uff1a\u8fd4\u56de\u901a\u8fc7\u7ed9\u5b9a\u51fd\u6570\u6d4b\u8bd5\u7684\u6570\u7ec4\u5143\u7d20\u3002<\/li>\n<li><strong>array_reduce()<\/strong>\uff1a\u5c06\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u7d2f\u79ef\u4e3a\u5355\u4e2a\u503c\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8ba9\u6211\u4eec\u770b\u4e00\u4e2a\u4f7f\u7528 PHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\u5b9e\u9645\u4f8b\u5b50\u3002\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u5b66\u751f\u5217\u8868\u53ca\u5176\u5206\u6570\u3002\u6211\u4eec\u7684\u76ee\u6807\u662f\u8ba1\u7b97\u6240\u6709\u53ca\u683c\u5206\u6570\u7684\u603b\u6570\u3002<\/p>\n<pre>&lt;?php\n\n$students = [\n    ['name' =&gt; 'John', 'score' =&gt; 85],\n    ['name' =&gt; 'Jane', 'score' =&gt; 90],\n    ['name' =&gt; 'Peter', 'score' =&gt; 75],\n    ['name' =&gt; 'Susan', 'score' =&gt; 80],\n];\n\n\/\/ \u4f7f\u7528 array_filter() \u8fc7\u6ee4\u6389\u5206\u6570\u4f4e\u4e8e 80 \u7684\u5b66\u751f\n$passingStudents = array_filter($students, function($student) {\n    return $student['score'] &gt;= 80;\n});\n\n\/\/ \u4f7f\u7528 array_map() \u5c06\u5b66\u751f\u5206\u6570\u63d0\u53d6\u5230\u65b0\u6570\u7ec4\u4e2d\n$scores = array_map(function($student) {\n    return $student['score'];\n}, $passingStudents);\n\n\/\/ \u4f7f\u7528 array_reduce() \u8ba1\u7b97\u5206\u6570\u603b\u548c\n$totalScore = array_reduce($scores, function($carry, $score) {\n    return $carry + $score;\n}, 0);\n\necho $totalScore;<\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>array_filter()<\/code> \u8fc7\u6ee4\u6389\u5206\u6570\u4f4e\u4e8e 80 \u7684\u5b66\u751f\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528 <code>array_map()<\/code> \u5c06\u5b66\u751f\u5206\u6570\u63d0\u53d6\u5230\u65b0\u6570\u7ec4\u4e2d\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528 <code>array_reduce()<\/code> \u8ba1\u7b97\u5206\u6570\u603b\u548c\u3002\u901a\u8fc7\u4f7f\u7528\u51fd\u6570\u5f0f\u7f16\u7a0b\u6280\u672f\uff0c\u6211\u4eec\u80fd\u591f\u4ee5\u4e00\u79cd\u6e05\u6670\u3001\u7b80\u6d01\u7684\u65b9\u5f0f\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300aPHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u521d\u5b66\u8005\u6559\u7a0b\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5f0f\u7f16\u7a0b\u6307\u5357\uff1a\u521d\u5b66\u8005\u6559&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-202432","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202432","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202432"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202432\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202432"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}