{"id":23506,"date":"2024-11-21T12:29:47","date_gmt":"2024-11-21T04:29:47","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23506\/"},"modified":"2024-11-21T12:29:47","modified_gmt":"2024-11-21T04:29:47","slug":"laravel%e5%bc%80%e5%8f%91%ef%bc%9a%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8laravel-collections%e6%93%8d%e4%bd%9c%e9%9b%86%e5%90%88%e6%95%b0%e6%8d%ae%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23506\/","title":{"rendered":"Laravel\u5f00\u53d1\uff1a\u5982\u4f55\u4f7f\u7528Laravel Collections\u64cd\u4f5c\u96c6\u5408\u6570\u636e\uff1f"},"content":{"rendered":"<p>\u5f00\u53d1\uff1a\u5982\u4f55\u4f7f\u7528laravel collections\u64cd\u4f5c\u96c6\u5408\u6570\u636e\uff1f<\/p>\n<p>\u5728Laravel\u5f00\u53d1\u4e2d\uff0c\u96c6\u5408(Collections)\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u4e14\u5b9e\u7528\u7684\u5de5\u5177\u3002\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u4f7f\u7528Laravel Collections\u6765\u65b9\u4fbf\u5730\u64cd\u4f5c\u548c\u5904\u7406\u96c6\u5408\u6570\u636e\u3002\u5728\u672c\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Laravel Collections\u64cd\u4f5c\u96c6\u5408\u6570\u636e\u3002<\/p>\n<p>1.\u4ec0\u4e48\u662f\u96c6\u5408(Collections)?<\/p>\n<p>\u96c6\u5408(Collections)\u662f\u4e00\u79cd\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u662fLaravel\u7684\u4e00\u4e2a\u7ec4\u4ef6\uff0c\u7528\u4e8e\u64cd\u4f5c\u6570\u7ec4\u6570\u636e\u3002\u5b83\u53ef\u4ee5\u50cf\u6570\u7ec4\u4e00\u6837\u8fdb\u884c\u8bbf\u95ee\u548c\u64cd\u4f5c\uff0c\u800c\u4e14\u8fd8\u5177\u6709\u8bb8\u591a\u6709\u7528\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982map()\uff0cfilter()\u548creduce()\u7b49\u65b9\u6cd5\u3002\u4f7f\u7528\u96c6\u5408\u53ef\u4ee5\u5e2e\u52a9\u60a8\u66f4\u8f7b\u677e\u5730\u5904\u7406\u548c\u64cd\u4f5c\u6570\u636e\u3002<\/p>\n<p>2.\u5982\u4f55\u521b\u5efa\u96c6\u5408(Collections)?<\/p>\n<p>\u5728Laravel\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528collect()\u51fd\u6570\u6216\u8005\u4f7f\u7528IlluminateSupportCollection\u7c7b\u6765\u521b\u5efa\u96c6\u5408\u5bf9\u8c61\u3002<\/p>\n<p>\u4f7f\u7528collect()\u51fd\u6570\u521b\u5efa\u96c6\u5408\u5bf9\u8c61\uff1a<\/p>\n<p>$collection = collect([1, 2, 3, 4, 5, 6]);<\/p>\n<p>\u4f7f\u7528IlluminateSupportCollection\u7c7b\u521b\u5efa\u96c6\u5408\u5bf9\u8c61\uff1a<\/p>\n<p>use IlluminateSupportCollection;<\/p>\n<p>$collection = new Collection([1, 2, 3, 4, 5, 6]);<\/p>\n<p>3.\u5982\u4f55\u4f7f\u7528\u96c6\u5408\u5bf9\u6570\u636e\u8fdb\u884c\u64cd\u4f5c?<\/p>\n<p>\u4e0b\u9762\u6211\u4eec\u5c06\u4ecb\u7ecd\u4f7f\u7528\u96c6\u5408\u5bf9\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u7684\u4e00\u4e9b\u5e38\u89c1\u65b9\u6cd5\u3002<\/p>\n<p>map()\u65b9\u6cd5\uff1a<\/p>\n<p>map()\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u5bf9\u96c6\u5408\u8fdb\u884c\u8f6c\u6362\u64cd\u4f5c\u3002\u5b83\u63a5\u53d7\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u5c06\u8be5\u51fd\u6570\u5e94\u7528\u4e8e\u96c6\u5408\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002\u7136\u540e\uff0c\u5c06\u8f6c\u6362\u540e\u7684\u5143\u7d20\u4f5c\u4e3a\u65b0\u7684\u96c6\u5408\u8fd4\u56de\u3002\u4f8b\u5982\uff1a<\/p>\n<p>$collection = collect([1, 2, 3, 4, 5]);<\/p>\n<p>$multiplied = $collection-&gt;map(function ($item, $key) {<\/p>\n<pre>return $item * 2;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>});<\/p>\n<p>\/\/ \u8f93\u51fa\uff1a[2, 4, 6, 8, 10]<br \/>$multiplied-&gt;all();<\/p>\n<p>filter()\u65b9\u6cd5\uff1a<\/p>\n<p>filter()\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u4ece\u96c6\u5408\u4e2d\u7b5b\u9009\u5143\u7d20\u3002\u5b83\u63a5\u53d7\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u5c06\u8be5\u51fd\u6570\u5e94\u7528\u4e8e\u96c6\u5408\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002\u5982\u679c\u8be5\u51fd\u6570\u8fd4\u56detrue\uff0c\u5219\u8be5\u5143\u7d20\u5c06\u4fdd\u7559\u5728\u96c6\u5408\u4e2d\u3002\u5426\u5219\uff0c\u8be5\u5143\u7d20\u5c06\u88ab\u5220\u9664\u3002\u4f8b\u5982\uff1a<\/p>\n<p>$collection = collect([1, 2, 3, 4, 5, 6]);<\/p>\n<p>$filtered = $collection-&gt;filter(function ($item) {<\/p>\n<pre>return $item &amp;gt; 3;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>});<\/p>\n<p>\/\/ \u8f93\u51fa\uff1a[4, 5, 6]<br \/>$filtered-&gt;all();<\/p>\n<p>reduce()\u65b9\u6cd5\uff1a<\/p>\n<p>reduce()\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u5bf9\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u8fed\u4ee3\u5904\u7406\u3002\u5b83\u63a5\u53d7\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u5c06\u8be5\u51fd\u6570\u5e94\u7528\u4e8e\u96c6\u5408\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002\u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1a\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u4e0a\u4e00\u4e2a\u5143\u7d20\u7684\u201c\u503c\u201d\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u5f53\u524d\u5143\u7d20\u7684\u503c\u3002\u5728\u8c03\u7528\u51fd\u6570\u4e4b\u540e\uff0c\u5c06\u8fd4\u56de\u4e00\u4e2a\u805a\u5408\u503c\uff0c\u8be5\u503c\u5c06\u4f20\u9012\u7ed9\u4e0b\u4e00\u4e2a\u5143\u7d20\u3002\u4f8b\u5982\uff1a<\/p>\n<p>$collection = collect([1, 2, 3, 4, 5]);<\/p>\n<p>$total = $collection-&gt;reduce(function ($carry, $item) {<\/p>\n<pre>return $carry + $item;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>});<\/p>\n<p>\/\/ \u8f93\u51fa\uff1a15<br \/>echo $total;<\/p>\n<p>sort()\u65b9\u6cd5\uff1a<\/p>\n<p>sort()\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u5bf9\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\u3002\u5b83\u4e0d\u4f1a\u6539\u53d8\u96c6\u5408\u672c\u8eab\uff0c\u800c\u662f\u8fd4\u56de\u6392\u5e8f\u540e\u7684\u65b0\u96c6\u5408\u3002\u4f8b\u5982\uff1a<\/p>\n<p>$collection = collect([5, 3, 1, 2, 4]);<\/p>\n<p>$sorted = $collection-&gt;sort();<\/p>\n<p>\/\/ \u8f93\u51fa\uff1a[1, 2, 3, 4, 5]<br \/>$sorted-&gt;all();<\/p>\n<p>groupBy()\u65b9\u6cd5\uff1a<\/p>\n<p>groupBy()\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u6309\u7167\u7279\u5b9a\u7684\u952e\u503c\u5bf9\u6765\u5bf9\u96c6\u5408\u8fdb\u884c\u5206\u7ec4\u3002\u4f8b\u5982\uff1a<\/p>\n<p>$collection = collect([<\/p>\n<pre>['product' =&amp;gt; 'Apple', 'price' =&amp;gt; 20],\n['product' =&amp;gt; 'Banana', 'price' =&amp;gt; 30],\n['product' =&amp;gt; 'Orange', 'price' =&amp;gt; 20],\n['product' =&amp;gt; 'Grapes', 'price' =&amp;gt; 40]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>]);<\/p>\n<p>$grouped = $collection-&gt;groupBy(&#8216;price&#8217;);<\/p>\n<p>\/\/ \u8f93\u51fa\uff1a<br \/>[<\/p>\n<pre>20 =&amp;gt; [\n    ['product' =&amp;gt; 'Apple', 'price' =&amp;gt; 20],\n    ['product' =&amp;gt; 'Orange', 'price' =&amp;gt; 20]\n],\n30 =&amp;gt; [\n    ['product' =&amp;gt; 'Banana', 'price' =&amp;gt; 30]\n],\n40 =&amp;gt; [\n    ['product' =&amp;gt; 'Grapes', 'price' =&amp;gt; 40]\n]<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>]<\/p>\n<p>4.\u603b\u7ed3<\/p>\n<p>\u672c\u6587\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Laravel Collections\u64cd\u4f5c\u96c6\u5408\u6570\u636e\u3002\u6211\u4eec\u8ba8\u8bba\u4e86\u4e00\u4e9b\u5e38\u7528\u7684\u96c6\u5408\u65b9\u6cd5\uff0c\u5305\u62ecmap()\uff0cfilter()\uff0creduce()\uff0csort()\u548cgroupBy()\u7b49\u3002\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u65b9\u4fbf\u5730\u5bf9\u96c6\u5408\u6570\u636e\u8fdb\u884c\u5904\u7406\u548c\u64cd\u4f5c\u3002\u5728\u8fdb\u884cLaravel\u5f00\u53d1\u65f6\uff0c\u96c6\u5408\u662f\u4e00\u4e2a\u975e\u5e38\u5b9e\u7528\u7684\u5de5\u5177\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fLaravel\u5f00\u53d1\uff1a\u5982\u4f55\u4f7f\u7528Laravel Collections\u64cd\u4f5c\u96c6\u5408\u6570\u636e\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f00\u53d1\uff1a\u5982\u4f55\u4f7f\u7528laravel collections\u64cd\u4f5c\u96c6\u5408\u6570\u636e\uff1f \u5728Laravel\u5f00\u53d1\u4e2d\uff0c\u96c6\u5408(Collections)\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u4e14\u5b9e\u7528\u7684\u5de5\u5177\u3002\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u4f7f\u7528Laravel Collections\u6765\u65b9\u4fbf\u5730\u64cd\u4f5c\u548c\u5904\u7406\u96c6\u5408\u6570\u636e\u3002\u5728\u672c\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Laravel Collections\u64cd\u4f5c\u96c6\u5408\u6570\u636e\u3002 1.\u4ec0\u4e48\u662f\u96c6\u5408(Collections)? \u96c6\u5408(Collections)\u662f\u4e00\u79cd\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u662fLaravel\u7684\u4e00\u4e2a\u7ec4\u4ef6\uff0c\u7528\u4e8e\u64cd\u4f5c\u6570\u7ec4\u6570\u636e\u3002\u5b83\u53ef\u4ee5\u50cf\u6570\u7ec4\u4e00\u6837\u8fdb\u884c\u8bbf\u95ee\u548c\u64cd\u4f5c\uff0c\u800c\u4e14\u8fd8\u5177\u6709\u8bb8\u591a\u6709\u7528\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982map()\uff0cfilter()\u548creduce()\u7b49\u65b9\u6cd5\u3002\u4f7f\u7528\u96c6\u5408\u53ef\u4ee5\u5e2e\u52a9\u60a8\u66f4\u8f7b\u677e\u5730\u5904\u7406\u548c\u64cd\u4f5c\u6570\u636e\u3002 2.\u5982\u4f55\u521b\u5efa\u96c6\u5408(Collections)? \u5728Laravel\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528collect()\u51fd\u6570\u6216\u8005\u4f7f\u7528IlluminateSupportCollection\u7c7b\u6765\u521b\u5efa\u96c6\u5408\u5bf9\u8c61\u3002 \u4f7f\u7528collect()\u51fd\u6570\u521b\u5efa\u96c6\u5408\u5bf9\u8c61\uff1a $collection = collect([1, 2, 3, 4, 5, 6]); \u4f7f\u7528IlluminateSupportCollection\u7c7b\u521b\u5efa\u96c6\u5408\u5bf9\u8c61\uff1a use IlluminateSupportCollection; $collection = new Collection([1, 2, 3, 4, 5, 6]); 3.\u5982\u4f55\u4f7f\u7528\u96c6\u5408\u5bf9\u6570\u636e\u8fdb\u884c\u64cd\u4f5c? \u4e0b\u9762\u6211\u4eec\u5c06\u4ecb\u7ecd\u4f7f\u7528\u96c6\u5408\u5bf9\u6570\u636e\u8fdb\u884c\u64cd\u4f5c\u7684\u4e00\u4e9b\u5e38\u89c1\u65b9\u6cd5\u3002 map()\u65b9\u6cd5\uff1a map()\u65b9\u6cd5\u53ef\u4ee5\u7528\u4e8e\u5bf9\u96c6\u5408\u8fdb\u884c\u8f6c\u6362\u64cd\u4f5c\u3002\u5b83\u63a5\u53d7\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u5c06\u8be5\u51fd\u6570\u5e94\u7528\u4e8e\u96c6\u5408\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u3002\u7136\u540e\uff0c\u5c06\u8f6c\u6362\u540e\u7684\u5143\u7d20\u4f5c\u4e3a\u65b0\u7684\u96c6\u5408\u8fd4\u56de\u3002\u4f8b\u5982\uff1a $collection = collect([1, 2, 3, 4, 5]); $multiplied = $collection-&gt;map(function ($item, $key) { return $item * 2; \u767b\u5f55\u540e\u590d\u5236 }); \/\/ \u8f93\u51fa\uff1a[2, [&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-23506","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23506","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=23506"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23506\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23506"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23506"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}