<article><h1>Laravel 的集合 Collection</h1><ul><li><a href="#introduction">简介</a><ul><li><a href="#creating-collections">创建集合</a></li></ul></li><li><a href="#available-methods">可用的方法</a></li><li><a href="#higher-order-messages">高阶信息传递</a></li></ul><p><a name="introduction"></a></p><h2><a href="#introduction">简介</a></h2><p><code class=" language-php">Illuminate\<span class="token package">Support<span class="token punctuation">\</span>Collection</span></code> 类提供一个流畅、便利的封装来操控数组数据。如下面的示例代码,我们用 <code class=" language-php">collect</code> 函数从数组中创建新的集合实例,对每一个元素运行 <code class=" language-php">strtoupper</code> 函数,然后移除所有的空元素:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'taylor'</span><span class="token punctuation">,</span> <span class="token string">'abigail'</span><span class="token punctuation">,</span> <span class="token keyword">null</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">map<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$name</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token function">strtoupper<span class="token punctuation">(</span></span><span class="token variable">$name</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token operator">-</span><span class="token operator">></span><span class="token function">reject<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$name</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token function">empty<span class="token punctuation">(</span></span><span class="token variable">$name</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>如上面的代码示例,<code class=" language-php">Collection</code> 类支持链式调用,一般来说,每一个 <code class=" language-php">Collection</code> 方法会返回一个全新的 <code class=" language-php">Collection</code> 实例,你可以放心地进行链接调用。</p><p><a name="creating-collections"></a></p><h3>创建集合</h3><p>如上所述,<code class=" language-php">collect</code> 辅助函数会利用传入的数组生成一个新的 <code class=" language-php">Illuminate\<span class="token package">Support<span class="token punctuation">\</span>Collection</span></code> 实例。所以要创建一个集合就这么简单:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><blockquote class="has-icon tip"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="56.6px" height="87.5px" viewBox="0 0 56.6 87.5" enable-background="new 0 0 56.6 87.5" xml:space="preserve"><path fill="#FFFFFF" d="M28.7 64.5c-1.4 0-2.5-1.1-2.5-2.5v-5.7 -5V41c0-1.4 1.1-2.5 2.5-2.5s2.5 1.1 2.5 2.5v10.1 5 5.8C31.2 63.4 30.1 64.5 28.7 64.5zM26.4 0.1C11.9 1 0.3 13.1 0 27.7c-0.1 7.9 3 15.2 8.2 20.4 0.5 0.5 0.8 1 1 1.7l3.1 13.1c0.3 1.1 1.3 1.9 2.4 1.9 0.3 0 0.7-0.1 1.1-0.2 1.1-0.5 1.6-1.8 1.4-3l-2-8.4 -0.4-1.8c-0.7-2.9-2-5.7-4-8 -1-1.2-2-2.5-2.7-3.9C5.8 35.3 4.7 30.3 5.4 25 6.7 14.5 15.2 6.3 25.6 5.1c13.9-1.5 25.8 9.4 25.8 23 0 4.1-1.1 7.9-2.9 11.2 -0.8 1.4-1.7 2.7-2.7 3.9 -2 2.3-3.3 5-4 8L41.4 53l-2 8.4c-0.3 1.2 0.3 2.5 1.4 3 0.3 0.2 0.7 0.2 1.1 0.2 1.1 0 2.2-0.8 2.4-1.9l3.1-13.1c0.2-0.6 0.5-1.2 1-1.7 5-5.1 8.2-12.1 8.2-19.8C56.4 12 42.8-1 26.4 0.1zM43.7 69.6c0 0.5-0.1 0.9-0.3 1.3 -0.4 0.8-0.7 1.6-0.9 2.5 -0.7 3-2 8.6-2 8.6 -1.3 3.2-4.4 5.5-7.9 5.5h-4.1H28h-0.5 -3.6c-3.5 0-6.7-2.4-7.9-5.7l-0.1-0.4 -1.8-7.8c-0.4-1.1-0.8-2.1-1.2-3.1 -0.1-0.3-0.2-0.5-0.2-0.9 0.1-1.3 1.3-2.1 2.6-2.1H41C42.4 67.5 43.6 68.2 43.7 69.6zM37.7 72.5H26.9c-4.2 0-7.2 3.9-6.3 7.9 0.6 1.3 1.8 2.1 3.2 2.1h4.1 0.5 0.5 3.6c1.4 0 2.7-0.8 3.2-2.1L37.7 72.5z"></path></svg></span></div> 默认 <a href="/docs/5.4/eloquent">Eloquent</a> 模型的查询结果总是以 <code class=" language-php">Collection</code> 实例返回。</p></blockquote><p><a name="available-methods"></a></p><h2><a href="#available-methods">可用的方法</a></h2><p>接下来,我们将会探讨 <code class=" language-php">Collection</code> 类的所有方法。要记得的是,所有方法都支持链式调用,几乎所有的方法都会返回新的 <code class=" language-php">Collection</code> 实例,让你保留原版的集合以备不时之需。</p><style>#collection-method-list > p {
column-count: 3; -moz-column-count: 3; -webkit-column-count: 3;
column-gap: 2em; -moz-column-gap: 2em; -webkit-column-gap: 2em;
}
#collection-method-list a {
display: block;
}</style><div id="collection-method-list"><p><a href="#method-all">all</a> <a href="#method-avg">avg</a> <a href="#method-chunk">chunk</a> <a href="#method-collapse">collapse</a> <a href="#method-combine">combine</a> <a href="#method-contains">contains</a> <a href="#method-count">count</a> <a href="#method-diff">diff</a> <a href="#method-diffkeys">diffKeys</a> <a href="#method-each">each</a> <a href="#method-every">every</a> <a href="#method-except">except</a> <a href="#method-filter">filter</a> <a href="#method-first">first</a> <a href="#method-flatmap">flatMap</a> <a href="#method-flatten">flatten</a> <a href="#method-flip">flip</a> <a href="#method-forget">forget</a> <a href="#method-forpage">forPage</a> <a href="#method-get">get</a> <a href="#method-groupby">groupBy</a> <a href="#method-has">has</a> <a href="#method-implode">implode</a> <a href="#method-intersect">intersect</a> <a href="#method-isempty">isEmpty</a> <a href="#method-keyby">keyBy</a> <a href="#method-keys">keys</a> <a href="#method-last">last</a> <a href="#method-map">map</a> <a href="#method-mapwithkeys">mapWithKeys</a> <a href="#method-max">max</a> <a href="#method-merge">merge</a> <a href="#method-min">min</a> <a href="#method-nth">nth</a> <a href="#method-only">only</a> <a href="#method-partition">partition</a> <a href="#method-pipe">pipe</a> <a href="#method-pluck">pluck</a> <a href="#method-pop">pop</a> <a href="#method-prepend">prepend</a> <a href="#method-pull">pull</a> <a href="#method-push">push</a> <a href="#method-put">put</a> <a href="#method-random">random</a> <a href="#method-reduce">reduce</a> <a href="#method-reject">reject</a> <a href="#method-reverse">reverse</a> <a href="#method-search">search</a> <a href="#method-shift">shift</a> <a href="#method-shuffle">shuffle</a> <a href="#method-slice">slice</a> <a href="#method-sort">sort</a> <a href="#method-sortby">sortBy</a> <a href="#method-sortbydesc">sortByDesc</a> <a href="#method-splice">splice</a> <a href="#method-split">split</a> <a href="#method-sum">sum</a> <a href="#method-take">take</a> <a href="#method-toarray">toArray</a> <a href="#method-tojson">toJson</a> <a href="#method-transform">transform</a> <a href="#method-union">union</a> <a href="#method-unique">unique</a> <a href="#method-values">values</a> <a href="#method-when">when</a> <a href="#method-where">where</a> <a href="#method-wherestrict">whereStrict</a> <a href="#method-wherein">whereIn</a> <a href="#method-whereinstrict">whereInStrict</a> <a href="#method-zip">zip</a></p></div><p><a name="method-listing"></a></p><h2><a href="#method-listing">方法清单</a></h2><style>#collection-method code {
font-size: 14px;
}
#collection-method:not(.first-collection-method) {
margin-top: 50px;
}</style><p><a name="method-all"></a></p><h4 id="collection-method" class="first-collection-method"><code class=" language-php"><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>返回该集合所代表的底层 <code class=" language-php">数组</code>:</p><pre class=" language-php"><code class=" language-php"><span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [1, 2, 3]</span></code></pre><p><a name="method-avg"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">avg<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>返回集合中所有项目的平均值:</p><pre class=" language-php"><code class=" language-php"><span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">avg<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// 3</span></code></pre><p>如果集合包含了嵌套数组或对象,你可以通过传递「键」来指定使用哪些值计算平均值:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span>
<span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'JavaScript: The Good Parts'</span><span class="token punctuation">,</span> <span class="token string">'pages'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">176</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'JavaScript: The Definitive Guide'</span><span class="token punctuation">,</span> <span class="token string">'pages'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">1096</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">avg<span class="token punctuation">(</span></span><span class="token string">'pages'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// 636</span></code></pre><p><a name="method-chunk"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">chunk<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>将集合拆成多个指定大小的较小集合:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$chunks</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">chunk<span class="token punctuation">(</span></span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$chunks</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">toArray<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [[1, 2, 3, 4], [5, 6, 7]]</span></code></pre><p>这个方法在适用于网格系统如 <a href="http://getbootstrap.com/css/#grid">Bootstrap</a> 的 <a href="/docs/5.4/views">视图</a> 。想像你有一个 <a href="/docs/5.4/eloquent">Eloquent</a> 模型的集合要显示在一个网格内:</p><pre class=" language-php"><code class=" language-php">@<span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$products</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">chunk<span class="token punctuation">(</span></span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token variable">$chunk</span><span class="token punctuation">)</span>
<span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>row<span class="token punctuation">"</span></span><span class="token punctuation">></span></span></span>
@<span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$chunk</span> <span class="token keyword">as</span> <span class="token variable">$product</span><span class="token punctuation">)</span>
<span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation"><</span>div</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">"</span>col-xs-4<span class="token punctuation">"</span></span><span class="token punctuation">></span></span></span><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token variable">$product</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">name</span> <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span></span>
@<span class="token keyword">endforeach</span>
<span class="token markup"><span class="token tag"><span class="token tag"><span class="token punctuation"></</span>div</span><span class="token punctuation">></span></span></span>
@<span class="token keyword">endforeach</span></code></pre><p><a name="method-collapse"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">collapse<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>将多个数组组成的集合合成单个一维数组集合:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collapsed</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">collapse<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collapsed</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [1, 2, 3, 4, 5, 6, 7, 8, 9]</span></code></pre><p><a name="method-combine"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">combine<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>将集合的值作为「键」,合并另一个数组或者集合作为「键」对应的值。</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'name'</span><span class="token punctuation">,</span> <span class="token string">'age'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$combined</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">combine<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'George'</span><span class="token punctuation">,</span> <span class="token number">29</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$combined</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// ['name' => 'George', 'age' => 29]</span></code></pre><p><a name="method-contains"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">contains<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>判断集合是否含有指定项目:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Desk'</span><span class="token punctuation">,</span> <span class="token string">'price'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">contains<span class="token punctuation">(</span></span><span class="token string">'Desk'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// true
</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">contains<span class="token punctuation">(</span></span><span class="token string">'New York'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// false</span></code></pre><p>你可以将一对键/值传入 <code class=" language-php">contains</code> 方法,用来判断该组合是否存在于集合内:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span>
<span class="token punctuation">[</span><span class="token string">'product'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Desk'</span><span class="token punctuation">,</span> <span class="token string">'price'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">200</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">'product'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Chair'</span><span class="token punctuation">,</span> <span class="token string">'price'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">contains<span class="token punctuation">(</span></span><span class="token string">'product'</span><span class="token punctuation">,</span> <span class="token string">'Bookcase'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// false</span></code></pre><p>最后,你也可以传入一个回调函数到 <code class=" language-php">contains</code> 方法内运行你自己的判断语句:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">contains<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token variable">$value</span> <span class="token operator">></span> <span class="token number">5</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// false</span></code></pre><p><a name="method-count"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">count<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>返回该集合内的项目总数:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">count<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// 4</span></code></pre><p><a name="method-diff"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">diff<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>将集合与其它集合或纯 PHP <code class=" language-php">数组</code> 进行值的比较,返回第一个集合中存在而第二个集合中不存在的值:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$diff</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">diff<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$diff</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [1, 3, 5]</span></code></pre><p><a name="method-diffkeys"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">diffKeys<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>将集合与其它集合或纯 PHP <code class=" language-php">数组</code> 的「键」进行比较,返回第一个集合中存在而第二个集合中不存在「键」所对应的键值对:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span>
<span class="token string">'one'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">10</span><span class="token punctuation">,</span>
<span class="token string">'two'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">20</span><span class="token punctuation">,</span>
<span class="token string">'three'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">30</span><span class="token punctuation">,</span>
<span class="token string">'four'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">40</span><span class="token punctuation">,</span>
<span class="token string">'five'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">50</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$diff</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">diffKeys<span class="token punctuation">(</span></span><span class="token punctuation">[</span>
<span class="token string">'two'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">2</span><span class="token punctuation">,</span>
<span class="token string">'four'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">4</span><span class="token punctuation">,</span>
<span class="token string">'six'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">6</span><span class="token punctuation">,</span>
<span class="token string">'eight'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">8</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$diff</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// ['one' => 10, 'three' => 30, 'five' => 50]</span></code></pre><p><a name="method-each"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">each<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>遍历集合中的项目,并将之传入回调函数:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">each<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$item</span><span class="token punctuation">,</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment" spellcheck="true"> //
</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>回调函数中返回 <code class=" language-php"><span class="token boolean">false</span></code> 以中断循环:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">each<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$item</span><span class="token punctuation">,</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token comment" spellcheck="true">/* some condition */</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-every"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">every<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>判断集合中每一个元素是否都符合指定条件:</p><pre class=" language-php"><code class=" language-php"><span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">every<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token variable">$value</span> <span class="token operator">></span> <span class="token number">2</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// false</span></code></pre><p><a name="method-except"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">except<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>返回集合中除了指定键以外的所有项目:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'product_id'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">'price'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token string">'discount'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token boolean">false</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$filtered</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">except<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'price'</span><span class="token punctuation">,</span> <span class="token string">'discount'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$filtered</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// ['product_id' => 1]</span></code></pre><p>与 <code class=" language-php">except</code> 相反的方法请查看 <a href="#method-only">only</a>。</p><p><a name="method-filter"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">filter<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>使用回调函数筛选集合,只留下那些通过判断测试的项目:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$filtered</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">filter<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token variable">$value</span> <span class="token operator">></span> <span class="token number">2</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$filtered</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [3, 4]</span></code></pre><p>如果没有提供回调函数,集合中所有返回 <code class=" language-php"><span class="token boolean">false</span></code> 的元素都会被移除:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token keyword">null</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">,</span> <span class="token string">''</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">filter<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [1, 2, 3]</span></code></pre><p>与 <code class=" language-php">filter</code> 相反的方法可以查看 <a href="#method-reject">reject</a>。</p><p><a name="method-first"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">first<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>返回集合第一个通过指定测试的元素:</p><pre class=" language-php"><code class=" language-php"><span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">first<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token variable">$value</span> <span class="token operator">></span> <span class="token number">2</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// 3</span></code></pre><p>你也可以不传入参数使用 <code class=" language-php">first</code> 方法以获取集合中第一个元素。如果集合是空的,则会返回 <code class=" language-php"><span class="token keyword">null</span></code>:</p><pre class=" language-php"><code class=" language-php"><span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">first<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// 1</span></code></pre><p><a name="method-flatmap"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">flatMap<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>对集合内所有子集遍历执行回调,并在最后转为一维集合:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span>
<span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Sally'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">'school'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Arkansas'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">'age'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">28</span><span class="token punctuation">]</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$flattened</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">flatMap<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$values</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token function">array_map<span class="token punctuation">(</span></span><span class="token string">'strtoupper'</span><span class="token punctuation">,</span> <span class="token variable">$values</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$flattened</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// ['name' => 'SALLY', 'school' => 'ARKANSAS', 'age' => '28'];</span></code></pre><p><a name="method-flatten"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">flatten<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>将多维集合转为一维集合:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'taylor'</span><span class="token punctuation">,</span> <span class="token string">'languages'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'php'</span><span class="token punctuation">,</span> <span class="token string">'javascript'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$flattened</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">flatten<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$flattened</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// ['taylor', 'php', 'javascript'];</span></code></pre><p>你可以选择性地传入遍历深度的参数:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span>
<span class="token string">'Apple'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span>
<span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'iPhone 6S'</span><span class="token punctuation">,</span> <span class="token string">'brand'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Apple'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token string">'Samsung'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span>
<span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Galaxy S7'</span><span class="token punctuation">,</span> <span class="token string">'brand'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Samsung'</span><span class="token punctuation">]</span>
<span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$products</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">flatten<span class="token punctuation">(</span></span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$products</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">values<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">/*
[
['name' => 'iPhone 6S', 'brand' => 'Apple'],
['name' => 'Galaxy S7', 'brand' => 'Samsung'],
]
*/</span></code></pre><p>在这个例子里,调用 <code class=" language-php">flatten</code> 方法时不传入深度参数会遍历嵌套数组降维成一维数组,生成 <code class=" language-php"><span class="token punctuation">[</span><span class="token string">'iPhone 6S'</span><span class="token punctuation">,</span> <span class="token string">'Apple'</span><span class="token punctuation">,</span> <span class="token string">'Galaxy S7'</span><span class="token punctuation">,</span> <span class="token string">'Samsung'</span><span class="token punctuation">]</span></code>,传入深度参数能让你限制降维嵌套数组的层数。</p><p><a name="method-flip"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">flip<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>将集合中的键和对应的数值进行互换:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'taylor'</span><span class="token punctuation">,</span> <span class="token string">'framework'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'laravel'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$flipped</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">flip<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$flipped</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// ['taylor' => 'name', 'laravel' => 'framework']</span></code></pre><p><a name="method-forget"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">forget<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>通过集合的键来移除掉集合中的一个项目:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'taylor'</span><span class="token punctuation">,</span> <span class="token string">'framework'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'laravel'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">forget<span class="token punctuation">(</span></span><span class="token string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// ['framework' => 'laravel']</span></code></pre><blockquote class="has-icon note"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="90px" height="90px" viewBox="0 0 90 90" enable-background="new 0 0 90 90" xml:space="preserve"><path fill="#FFFFFF" d="M45 0C20.1 0 0 20.1 0 45s20.1 45 45 45 45-20.1 45-45S69.9 0 45 0zM45 74.5c-3.6 0-6.5-2.9-6.5-6.5s2.9-6.5 6.5-6.5 6.5 2.9 6.5 6.5S48.6 74.5 45 74.5zM52.1 23.9l-2.5 29.6c0 2.5-2.1 4.6-4.6 4.6 -2.5 0-4.6-2.1-4.6-4.6l-2.5-29.6c-0.1-0.4-0.1-0.7-0.1-1.1 0-4 3.2-7.2 7.2-7.2 4 0 7.2 3.2 7.2 7.2C52.2 23.1 52.2 23.5 52.1 23.9z"></path></svg></span></div> 与大多数其它集合的方法不同,<code class=" language-php">forget</code> 不会返回修改过后的新集合;它会直接修改调用它的集合。</p></blockquote><p><a name="method-forpage"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">forPage<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>返回可用来在指定页码上所显示项目的新集合。这个方法第一个参数是页码数,第二个参数是每页显示的个数。</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$chunk</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">forPage<span class="token punctuation">(</span></span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$chunk</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [4, 5, 6]</span></code></pre><p><a name="method-get"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">get<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>返回指定键的项目。如果该键不存在,则返回 <code class=" language-php"><span class="token keyword">null</span></code>:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'taylor'</span><span class="token punctuation">,</span> <span class="token string">'framework'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'laravel'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$value</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">get<span class="token punctuation">(</span></span><span class="token string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// taylor</span></code></pre><p>你可以选择性地传入一个默认值作为第二个参数:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'taylor'</span><span class="token punctuation">,</span> <span class="token string">'framework'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'laravel'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$value</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">get<span class="token punctuation">(</span></span><span class="token string">'foo'</span><span class="token punctuation">,</span> <span class="token string">'default-value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// default-value</span></code></pre><p>你甚至可以传入回调函数当默认值。如果指定的键不存在,就会返回回调函数的运行结果:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">get<span class="token punctuation">(</span></span><span class="token string">'email'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token string">'default-value'</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// default-value</span></code></pre><p><a name="method-groupby"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">groupBy<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>根据指定的「键」为集合内的项目分组:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span>
<span class="token punctuation">[</span><span class="token string">'account_id'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'account-x10'</span><span class="token punctuation">,</span> <span class="token string">'product'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Chair'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">'account_id'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'account-x10'</span><span class="token punctuation">,</span> <span class="token string">'product'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Bookcase'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">'account_id'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'account-x11'</span><span class="token punctuation">,</span> <span class="token string">'product'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Desk'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$grouped</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">groupBy<span class="token punctuation">(</span></span><span class="token string">'account_id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$grouped</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">toArray<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">/*
[
'account-x10' => [
['account_id' => 'account-x10', 'product' => 'Chair'],
['account_id' => 'account-x10', 'product' => 'Bookcase'],
],
'account-x11' => [
['account_id' => 'account-x11', 'product' => 'Desk'],
],
]
*/</span></code></pre><p>除了传入字符串的「键」之外,你也可以传入回调函数。该函数应该返回你希望用来分组的键的值。</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$grouped</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">groupBy<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$item</span><span class="token punctuation">,</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token function">substr<span class="token punctuation">(</span></span><span class="token variable">$item</span><span class="token punctuation">[</span><span class="token string">'account_id'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$grouped</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">toArray<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">/*
[
'x10' => [
['account_id' => 'account-x10', 'product' => 'Chair'],
['account_id' => 'account-x10', 'product' => 'Bookcase'],
],
'x11' => [
['account_id' => 'account-x11', 'product' => 'Desk'],
],
]
*/</span></code></pre><p><a name="method-has"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">has<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>检查集合中是否含有指定的「键」:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'account_id'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">'product'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Desk'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">has<span class="token punctuation">(</span></span><span class="token string">'product'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// true</span></code></pre><p><a name="method-implode"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">implode<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">implode</code> 方法合并集合中的项目。它的参数依集合中的项目类型而定。假如集合含有数组或对象,你应该传入你希望连接的属性的「键」,以及你希望放在数值之间的拼接字符串:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span>
<span class="token punctuation">[</span><span class="token string">'account_id'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">'product'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Desk'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">'account_id'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">'product'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Chair'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">implode<span class="token punctuation">(</span></span><span class="token string">'product'</span><span class="token punctuation">,</span> <span class="token string">', '</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// Desk, Chair</span></code></pre><p>假如集合只含有简单的字符串或数字,则只需要传入拼接的字符串作为该方法的唯一参数即可:</p><pre class=" language-php"><code class=" language-php"><span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">implode<span class="token punctuation">(</span></span><span class="token string">'-'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// '1-2-3-4-5'</span></code></pre><p><a name="method-intersect"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">intersect<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>移除任何指定 <code class=" language-php">数组</code> 或集合内所没有的数值。最终集合保存着原集合的键:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'Desk'</span><span class="token punctuation">,</span> <span class="token string">'Sofa'</span><span class="token punctuation">,</span> <span class="token string">'Chair'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$intersect</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">intersect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'Desk'</span><span class="token punctuation">,</span> <span class="token string">'Chair'</span><span class="token punctuation">,</span> <span class="token string">'Bookcase'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$intersect</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [0 => 'Desk', 2 => 'Chair']</span></code></pre><p><a name="method-isempty"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">isEmpty<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>如果集合是空的,<code class=" language-php">isEmpty</code> 方法会返回 <code class=" language-php"><span class="token boolean">true</span></code>:否则返回 <code class=" language-php"><span class="token boolean">false</span></code>:</p><pre class=" language-php"><code class=" language-php"><span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">isEmpty<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// true</span></code></pre><p><a name="method-keyby"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">keyBy<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>以指定键的值作为集合项目的键。如果几个数据项有相同的键,那在新集合中只显示最后一项:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span>
<span class="token punctuation">[</span><span class="token string">'product_id'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'prod-100'</span><span class="token punctuation">,</span> <span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'desk'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">'product_id'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'prod-200'</span><span class="token punctuation">,</span> <span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'chair'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$keyed</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">keyBy<span class="token punctuation">(</span></span><span class="token string">'product_id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$keyed</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">/*
[
'prod-100' => ['product_id' => 'prod-100', 'name' => 'Desk'],
'prod-200' => ['product_id' => 'prod-200', 'name' => 'Chair'],
]
*/</span></code></pre><p>你也可以传入自己的回调函数,该函数应该返回集合的键的值:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$keyed</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">keyBy<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$item</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token function">strtoupper<span class="token punctuation">(</span></span><span class="token variable">$item</span><span class="token punctuation">[</span><span class="token string">'product_id'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$keyed</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">/*
[
'PROD-100' => ['product_id' => 'prod-100', 'name' => 'Desk'],
'PROD-200' => ['product_id' => 'prod-200', 'name' => 'Chair'],
]
*/</span></code></pre><p><a name="method-keys"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">keys<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>返回该集合所有的键:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span>
<span class="token string">'prod-100'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'product_id'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'prod-100'</span><span class="token punctuation">,</span> <span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Desk'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token string">'prod-200'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'product_id'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'prod-200'</span><span class="token punctuation">,</span> <span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Chair'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$keys</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">keys<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$keys</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// ['prod-100', 'prod-200']</span></code></pre><p><a name="method-last"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">last<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>返回集合中,最后一个通过指定测试的元素:</p><pre class=" language-php"><code class=" language-php"><span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">last<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token variable">$value</span> <span class="token operator"><</span> <span class="token number">3</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// 2</span></code></pre><p>你也可以不传入参数使用 <code class=" language-php">last</code> 方法以获取集合中最后一个元素。如果集合是空的,则会返回 <code class=" language-php"><span class="token keyword">null</span></code>:</p><pre class=" language-php"><code class=" language-php"><span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">last<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// 4</span></code></pre><p><a name="method-map"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">map<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>遍历整个集合并将每一个数值传入回调函数。回调函数可以任意修改并返回项目,形成修改过的项目组成的新集合:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$multiplied</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">map<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$item</span><span class="token punctuation">,</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token variable">$item</span> <span class="token operator">*</span> <span class="token number">2</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$multiplied</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [2, 4, 6, 8, 10]</span></code></pre><blockquote class="has-icon note"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="90px" height="90px" viewBox="0 0 90 90" enable-background="new 0 0 90 90" xml:space="preserve"><path fill="#FFFFFF" d="M45 0C20.1 0 0 20.1 0 45s20.1 45 45 45 45-20.1 45-45S69.9 0 45 0zM45 74.5c-3.6 0-6.5-2.9-6.5-6.5s2.9-6.5 6.5-6.5 6.5 2.9 6.5 6.5S48.6 74.5 45 74.5zM52.1 23.9l-2.5 29.6c0 2.5-2.1 4.6-4.6 4.6 -2.5 0-4.6-2.1-4.6-4.6l-2.5-29.6c-0.1-0.4-0.1-0.7-0.1-1.1 0-4 3.2-7.2 7.2-7.2 4 0 7.2 3.2 7.2 7.2C52.2 23.1 52.2 23.5 52.1 23.9z"></path></svg></span></div> 正如集合大多数其它的方法一样,<code class=" language-php">map</code> 返回一个新集合实例;它并没有修改被调用的集合。假如你想改变原始的集合,得使用 <a href="#method-transform"><code class=" language-php">transform</code></a> 方法。</p></blockquote><p><a name="method-mapwithkeys"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">mapWithKeys<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>遍历整个集合并将每一个数值传入回调函数。回调函数返回包含一个键值对的关联数组:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span>
<span class="token punctuation">[</span>
<span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'John'</span><span class="token punctuation">,</span>
<span class="token string">'department'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Sales'</span><span class="token punctuation">,</span>
<span class="token string">'email'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'john@example.com'</span>
<span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span>
<span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Jane'</span><span class="token punctuation">,</span>
<span class="token string">'department'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Marketing'</span><span class="token punctuation">,</span>
<span class="token string">'email'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'jane@example.com'</span>
<span class="token punctuation">]</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$keyed</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">mapWithKeys<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$item</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token punctuation">[</span><span class="token variable">$item</span><span class="token punctuation">[</span><span class="token string">'email'</span><span class="token punctuation">]</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token variable">$item</span><span class="token punctuation">[</span><span class="token string">'name'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$keyed</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">/*
[
'john@example.com' => 'John',
'jane@example.com' => 'Jane',
]
*/</span></code></pre><p><a name="method-max"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">max<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>计算指定键的最大值:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$max</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token string">'foo'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'foo'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">20</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">max<span class="token punctuation">(</span></span><span class="token string">'foo'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// 20
</span>
<span class="token variable">$max</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">max<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// 5</span></code></pre><p><a name="method-merge"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">merge<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>合并数组进集合。数组「键」对应的数值会覆盖集合「键」对应的数值:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'product_id'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">'price'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$merged</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">merge<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'price'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token string">'discount'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token boolean">false</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$merged</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// ['product_id' => 1, 'price' => 200, 'discount' => false]</span></code></pre><p>如果指定数组的「键」为数字,则「值」将会合并到集合的后面:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'Desk'</span><span class="token punctuation">,</span> <span class="token string">'Chair'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$merged</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">merge<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'Bookcase'</span><span class="token punctuation">,</span> <span class="token string">'Door'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$merged</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// ['Desk', 'Chair', 'Bookcase', 'Door']</span></code></pre><p><a name="method-min"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">min<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>计算指定「键」的最小值:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$min</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token string">'foo'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'foo'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">20</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">min<span class="token punctuation">(</span></span><span class="token string">'foo'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// 10
</span>
<span class="token variable">$min</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">min<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// 1</span></code></pre><p><a name="method-nth"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">nth<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>由每隔第 n 个元素组成一个新的集合:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'b'</span><span class="token punctuation">,</span> <span class="token string">'c'</span><span class="token punctuation">,</span> <span class="token string">'d'</span><span class="token punctuation">,</span> <span class="token string">'e'</span><span class="token punctuation">,</span> <span class="token string">'f'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">nth<span class="token punctuation">(</span></span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// ['a', 'e']</span></code></pre><p>你也可以选择传入一个偏移量作为第二个参数</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">nth<span class="token punctuation">(</span></span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// ['b', 'f']</span></code></pre><p><a name="method-only"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">only<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>返回集合中指定键的所有项目:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'product_id'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Desk'</span><span class="token punctuation">,</span> <span class="token string">'price'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token string">'discount'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token boolean">false</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$filtered</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">only<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'product_id'</span><span class="token punctuation">,</span> <span class="token string">'name'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$filtered</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// ['product_id' => 1, 'name' => 'Desk']</span></code></pre><p>与 <code class=" language-php">only</code> 相反的方法请查看 <a href="#method-only">except</a>。</p><p><a name="method-partition"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">partition<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>结合 PHP 中的 <code class=" language-php">list</code> 方法来分开符合指定条件的元素以及那些不符合指定条件的元素:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">list<span class="token punctuation">(</span></span><span class="token variable">$underThree</span><span class="token punctuation">,</span> <span class="token variable">$aboveThree</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">partition<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$i</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token variable">$i</span> <span class="token operator"><</span> <span class="token number">3</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p><a name="method-pipe"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">pipe<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>将集合传给回调函数并返回结果:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$piped</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">pipe<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$collection</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">sum<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// 6</span></code></pre><p><a name="method-pluck"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">pluck<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>获取集合中指定「键」所有对应的值:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span>
<span class="token punctuation">[</span><span class="token string">'product_id'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'prod-100'</span><span class="token punctuation">,</span> <span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Desk'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">'product_id'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'prod-200'</span><span class="token punctuation">,</span> <span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Chair'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$plucked</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">pluck<span class="token punctuation">(</span></span><span class="token string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$plucked</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// ['Desk', 'Chair']</span></code></pre><p>你也可以指定最终集合的键:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$plucked</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">pluck<span class="token punctuation">(</span></span><span class="token string">'name'</span><span class="token punctuation">,</span> <span class="token string">'product_id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$plucked</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// ['prod-100' => 'Desk', 'prod-200' => 'Chair']</span></code></pre><p><a name="method-pop"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">pop<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>移除并返回集合最后一个项目:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">pop<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// 5
</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [1, 2, 3, 4]</span></code></pre><p><a name="method-prepend"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">prepend<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>在集合前面增加一项数组的值:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">prepend<span class="token punctuation">(</span></span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [0, 1, 2, 3, 4, 5]</span></code></pre><p>你可以传递第二个参数来设置新增加项的键:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'one'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">'two'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">prepend<span class="token punctuation">(</span></span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token string">'zero'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// ['zero' => 0, 'one' => 1, 'two' => 2]</span></code></pre><p><a name="method-pull"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">pull<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>把「键」对应的值从集合中移除并返回:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'product_id'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'prod-100'</span><span class="token punctuation">,</span> <span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Desk'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">pull<span class="token punctuation">(</span></span><span class="token string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// 'Desk'
</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// ['product_id' => 'prod-100']</span></code></pre><p><a name="method-push"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">push<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>在集合的后面新添加一个元素:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">push<span class="token punctuation">(</span></span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [1, 2, 3, 4, 5]</span></code></pre><p><a name="method-put"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">put<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>在集合内设置一个「键/值」:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'product_id'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Desk'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">put<span class="token punctuation">(</span></span><span class="token string">'price'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// ['product_id' => 1, 'name' => 'Desk', 'price' => 100]</span></code></pre><p><a name="method-random"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">random<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">random</code> 方法从集合中随机返回一个项目:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">random<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// 4 - (retrieved randomly)</span></code></pre><p>你可以选择性地传入一个整数到 <code class=" language-php">random</code>。如果该整数大于 <code class=" language-php"><span class="token number">1</span></code>,则会返回一个集合:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$random</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">random<span class="token punctuation">(</span></span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$random</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [2, 4, 5] - (retrieved randomly)</span></code></pre><p><a name="method-reduce"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">reduce<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">reduce</code> 方法将集合缩减到单个数值,该方法会将每次迭代的结果传入到下一次迭代:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$total</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">reduce<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$carry</span><span class="token punctuation">,</span> <span class="token variable">$item</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token variable">$carry</span> <span class="token operator">+</span> <span class="token variable">$item</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// 6</span></code></pre><p>第一次迭代时 <code class=" language-php"><span class="token variable">$carry</span></code> 的数值为 <code class=" language-php"><span class="token keyword">null</span></code>;然而你也可以传入第二个参数进 <code class=" language-php">reduce</code> 以指定它的初始值:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">reduce<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$carry</span><span class="token punctuation">,</span> <span class="token variable">$item</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token variable">$carry</span> <span class="token operator">+</span> <span class="token variable">$item</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// 10</span></code></pre><p><a name="method-reject"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">reject<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">reject</code> 方法以指定的回调函数筛选集合。会移除掉那些通过判断测试(即结果返回 <code class=" language-php"><span class="token boolean">true</span></code>)的项目:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$filtered</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">reject<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token variable">$value</span> <span class="token operator">></span> <span class="token number">2</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$filtered</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [1, 2]</span></code></pre><p>与 <code class=" language-php">reject</code> 相反的方法可以查看 <a href="#method-filter"><code class=" language-php">filter</code></a> 方法。</p><p><a name="method-reverse"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">reverse<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">reverse</code> 方法倒转集合内项目的顺序:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$reversed</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">reverse<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$reversed</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [5, 4, 3, 2, 1]</span></code></pre><p><a name="method-search"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">search<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">search</code> 方法在集合内搜索指定的数值并返回找到的键。假如找不到项目,则返回 <code class=" language-php"><span class="token boolean">false</span></code>:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">search<span class="token punctuation">(</span></span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// 1</span></code></pre><p>搜索是用「宽松」匹配来进行,也就是说如果字符串值是整数那它就跟这个整数是相等的。要使用严格匹配的话,就传入 <code class=" language-php"><span class="token boolean">true</span></code> 为该方法的第二个参数:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">search<span class="token punctuation">(</span></span><span class="token string">'4'</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// false</span></code></pre><p>另外,你可以传入你自己的回调函数来搜索第一个通过你判断测试的项目:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">search<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$item</span><span class="token punctuation">,</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token variable">$item</span> <span class="token operator">></span> <span class="token number">5</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// 2</span></code></pre><p><a name="method-shift"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">shift<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">shift</code> 方法移除并返回集合的第一个项目:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">shift<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// 1
</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [2, 3, 4, 5]</span></code></pre><p><a name="method-shuffle"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">shuffle<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">shuffle</code> 方法随机排序集合的项目:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$shuffled</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">shuffle<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$shuffled</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [3, 2, 5, 1, 4] // (generated randomly)</span></code></pre><p><a name="method-slice"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">slice<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">slice</code> 方法返回集合从指定索引开始的一部分切片:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$slice</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">slice<span class="token punctuation">(</span></span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$slice</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [5, 6, 7, 8, 9, 10]</span></code></pre><p>如果你想限制返回切片的大小,就传入想要的大小为方法的第二个参数:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$slice</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">slice<span class="token punctuation">(</span></span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$slice</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [5, 6]</span></code></pre><p>返回的切片将会保留原始键作为索引。假如你不希望保留原始的键,你可以使用 <code class=" language-php">values</code> 方法来重新建立索引。</p><p><a name="method-sort"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">sort<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>对集合排序。排序后的集合保留着原始数组的键,所以在这个例子里我们用 <a href="#method-values"><code class=" language-php">values</code></a> 方法来把键设置为连续数字的键。</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$sorted</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">sort<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$sorted</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">values<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [1, 2, 3, 4, 5]</span></code></pre><p>假如你需要更高级的排序,你可以传入回调函数以你自己的算法进行<code class=" language-php">排序</code>。参考 PHP 文档的 <a href="http://php.net/manual/en/function.usort.php#refsect1-function.usort-parameters"><code class=" language-php">usort</code></a>,这是集合的 <code class=" language-php">sort</code> 方法在背后所调用的函数。</p><blockquote class="has-icon tip"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="56.6px" height="87.5px" viewBox="0 0 56.6 87.5" enable-background="new 0 0 56.6 87.5" xml:space="preserve"><path fill="#FFFFFF" d="M28.7 64.5c-1.4 0-2.5-1.1-2.5-2.5v-5.7 -5V41c0-1.4 1.1-2.5 2.5-2.5s2.5 1.1 2.5 2.5v10.1 5 5.8C31.2 63.4 30.1 64.5 28.7 64.5zM26.4 0.1C11.9 1 0.3 13.1 0 27.7c-0.1 7.9 3 15.2 8.2 20.4 0.5 0.5 0.8 1 1 1.7l3.1 13.1c0.3 1.1 1.3 1.9 2.4 1.9 0.3 0 0.7-0.1 1.1-0.2 1.1-0.5 1.6-1.8 1.4-3l-2-8.4 -0.4-1.8c-0.7-2.9-2-5.7-4-8 -1-1.2-2-2.5-2.7-3.9C5.8 35.3 4.7 30.3 5.4 25 6.7 14.5 15.2 6.3 25.6 5.1c13.9-1.5 25.8 9.4 25.8 23 0 4.1-1.1 7.9-2.9 11.2 -0.8 1.4-1.7 2.7-2.7 3.9 -2 2.3-3.3 5-4 8L41.4 53l-2 8.4c-0.3 1.2 0.3 2.5 1.4 3 0.3 0.2 0.7 0.2 1.1 0.2 1.1 0 2.2-0.8 2.4-1.9l3.1-13.1c0.2-0.6 0.5-1.2 1-1.7 5-5.1 8.2-12.1 8.2-19.8C56.4 12 42.8-1 26.4 0.1zM43.7 69.6c0 0.5-0.1 0.9-0.3 1.3 -0.4 0.8-0.7 1.6-0.9 2.5 -0.7 3-2 8.6-2 8.6 -1.3 3.2-4.4 5.5-7.9 5.5h-4.1H28h-0.5 -3.6c-3.5 0-6.7-2.4-7.9-5.7l-0.1-0.4 -1.8-7.8c-0.4-1.1-0.8-2.1-1.2-3.1 -0.1-0.3-0.2-0.5-0.2-0.9 0.1-1.3 1.3-2.1 2.6-2.1H41C42.4 67.5 43.6 68.2 43.7 69.6zM37.7 72.5H26.9c-4.2 0-7.2 3.9-6.3 7.9 0.6 1.3 1.8 2.1 3.2 2.1h4.1 0.5 0.5 3.6c1.4 0 2.7-0.8 3.2-2.1L37.7 72.5z"></path></svg></span></div> 要排序嵌套数组或对象的集合,见 <a href="#method-sortby"><code class=" language-php">sortBy</code></a> 和 <a href="#method-sortbydesc"><code class=" language-php">sortByDesc</code></a> 方法。</p></blockquote><p><a name="method-sortby"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">sortBy<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>以指定的键排序集合。排序后的集合保留了原始数组键,所以在这个例子中我们用 <a href="#method-values"><code class=" language-php">values</code></a> method 把键设置为连续数字的索引建:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span>
<span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Desk'</span><span class="token punctuation">,</span> <span class="token string">'price'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">200</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Chair'</span><span class="token punctuation">,</span> <span class="token string">'price'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Bookcase'</span><span class="token punctuation">,</span> <span class="token string">'price'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">150</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$sorted</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">sortBy<span class="token punctuation">(</span></span><span class="token string">'price'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$sorted</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">values<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">/*
[
['name' => 'Chair', 'price' => 100],
['name' => 'Bookcase', 'price' => 150],
['name' => 'Desk', 'price' => 200],
]
*/</span></code></pre><p>你也可以传入自己的回调函数以决定如何排序集合数值:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span>
<span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Desk'</span><span class="token punctuation">,</span> <span class="token string">'colors'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'Black'</span><span class="token punctuation">,</span> <span class="token string">'Mahogany'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Chair'</span><span class="token punctuation">,</span> <span class="token string">'colors'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'Black'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Bookcase'</span><span class="token punctuation">,</span> <span class="token string">'colors'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'Red'</span><span class="token punctuation">,</span> <span class="token string">'Beige'</span><span class="token punctuation">,</span> <span class="token string">'Brown'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$sorted</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">sortBy<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$product</span><span class="token punctuation">,</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token function">count<span class="token punctuation">(</span></span><span class="token variable">$product</span><span class="token punctuation">[</span><span class="token string">'colors'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$sorted</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">values<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">/*
[
['name' => 'Chair', 'colors' => ['Black']],
['name' => 'Desk', 'colors' => ['Black', 'Mahogany']],
['name' => 'Bookcase', 'colors' => ['Red', 'Beige', 'Brown']],
]
*/</span></code></pre><p><a name="method-sortbydesc"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">sortByDesc<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>与 <a href="#method-sortby"><code class=" language-php">sortBy</code></a> 有着一样的形式,但是会以相反的顺序来排序集合:</p><p><a name="method-splice"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">splice<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>返回从指定的索引开始的一小切片项目,原本集合也会被切除:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$chunk</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">splice<span class="token punctuation">(</span></span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$chunk</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [3, 4, 5]
</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [1, 2]</span></code></pre><p>你可以传入第二个参数以限制大小:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$chunk</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">splice<span class="token punctuation">(</span></span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$chunk</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [3]
</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [1, 2, 4, 5]</span></code></pre><p>此外,你可以传入含有新项目的第三个参数以取代集合中被移除的项目:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$chunk</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">splice<span class="token punctuation">(</span></span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$chunk</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [3]
</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [1, 2, 10, 11, 4, 5]</span></code></pre><p><a name="method-split"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">split<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>将集合按指定组数分解:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$groups</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">split<span class="token punctuation">(</span></span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$groups</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">toArray<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [[1, 2], [3, 4], [5]]</span></code></pre><p><a name="method-sum"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">sum<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>返回集合内所有项目的总和:</p><pre class=" language-php"><code class=" language-php"><span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">sum<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// 15</span></code></pre><p>如果集合包含嵌套数组或对象,你应该传入一个「键」来指定要用哪些数值来计算总和:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span>
<span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'JavaScript: The Good Parts'</span><span class="token punctuation">,</span> <span class="token string">'pages'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">176</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'JavaScript: The Definitive Guide'</span><span class="token punctuation">,</span> <span class="token string">'pages'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">1096</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">sum<span class="token punctuation">(</span></span><span class="token string">'pages'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// 1272</span></code></pre><p>此外,你可以传入自己的回调函数来决定要用哪些数值来计算总和:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span>
<span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Chair'</span><span class="token punctuation">,</span> <span class="token string">'colors'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'Black'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Desk'</span><span class="token punctuation">,</span> <span class="token string">'colors'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'Black'</span><span class="token punctuation">,</span> <span class="token string">'Mahogany'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Bookcase'</span><span class="token punctuation">,</span> <span class="token string">'colors'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'Red'</span><span class="token punctuation">,</span> <span class="token string">'Beige'</span><span class="token punctuation">,</span> <span class="token string">'Brown'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">sum<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$product</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token function">count<span class="token punctuation">(</span></span><span class="token variable">$product</span><span class="token punctuation">[</span><span class="token string">'colors'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// 6</span></code></pre><p><a name="method-take"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">take<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>返回有着指定数量项目的集合:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$chunk</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">take<span class="token punctuation">(</span></span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$chunk</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [0, 1, 2]</span></code></pre><p>你也可以传入负整数以获取从集合后面来算指定数量的项目:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$chunk</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">take<span class="token punctuation">(</span></span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$chunk</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [4, 5]</span></code></pre><p><a name="method-toarray"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">toArray<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>将集合转换成纯 PHP <code class=" language-php">数组</code>。假如集合的数值是 <a href="/docs/5.4/eloquent">Eloquent</a> 模型,也会被转换成数组:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Desk'</span><span class="token punctuation">,</span> <span class="token string">'price'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">200</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">toArray<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">/*
[
['name' => 'Desk', 'price' => 200],
]
*/</span></code></pre><blockquote class="has-icon note"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="90px" height="90px" viewBox="0 0 90 90" enable-background="new 0 0 90 90" xml:space="preserve"><path fill="#FFFFFF" d="M45 0C20.1 0 0 20.1 0 45s20.1 45 45 45 45-20.1 45-45S69.9 0 45 0zM45 74.5c-3.6 0-6.5-2.9-6.5-6.5s2.9-6.5 6.5-6.5 6.5 2.9 6.5 6.5S48.6 74.5 45 74.5zM52.1 23.9l-2.5 29.6c0 2.5-2.1 4.6-4.6 4.6 -2.5 0-4.6-2.1-4.6-4.6l-2.5-29.6c-0.1-0.4-0.1-0.7-0.1-1.1 0-4 3.2-7.2 7.2-7.2 4 0 7.2 3.2 7.2 7.2C52.2 23.1 52.2 23.5 52.1 23.9z"></path></svg></span></div> <code class=" language-php">toArray</code> 也会转换所有内嵌的对象为数组。假如你希望获取原本的底层数组,改用 <a href="#method-all"><code class=" language-php">all</code></a> 方法。</p></blockquote><p><a name="method-tojson"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">toJson<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>将集合转换成 JSON:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Desk'</span><span class="token punctuation">,</span> <span class="token string">'price'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">200</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">toJson<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// '{"name":"Desk", "price":200}'</span></code></pre><p><a name="method-transform"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">transform<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>遍历集合并对集合内每一个项目调用指定的回调函数。集合的项目将会被回调函数返回的数值取代掉:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">transform<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$item</span><span class="token punctuation">,</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token variable">$item</span> <span class="token operator">*</span> <span class="token number">2</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [2, 4, 6, 8, 10]</span></code></pre><blockquote class="has-icon note"><p><div class="flag"><span class="svg"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/" version="1.1" x="0px" y="0px" width="90px" height="90px" viewBox="0 0 90 90" enable-background="new 0 0 90 90" xml:space="preserve"><path fill="#FFFFFF" d="M45 0C20.1 0 0 20.1 0 45s20.1 45 45 45 45-20.1 45-45S69.9 0 45 0zM45 74.5c-3.6 0-6.5-2.9-6.5-6.5s2.9-6.5 6.5-6.5 6.5 2.9 6.5 6.5S48.6 74.5 45 74.5zM52.1 23.9l-2.5 29.6c0 2.5-2.1 4.6-4.6 4.6 -2.5 0-4.6-2.1-4.6-4.6l-2.5-29.6c-0.1-0.4-0.1-0.7-0.1-1.1 0-4 3.2-7.2 7.2-7.2 4 0 7.2 3.2 7.2 7.2C52.2 23.1 52.2 23.5 52.1 23.9z"></path></svg></span></div> 与大多数其它集合的方法不同,<code class=" language-php">transform</code> 会修改集合本身。如果你希望创建新集合,就改用 <a href="#method-map"><code class=" language-php">map</code></a> 方法。</p></blockquote><p><a name="method-union"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">union<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>将给定的数组合并到集合中,如果数组中含有与集合一样的「键」,集合的键值会被保留:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'a'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token number">2</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'b'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$union</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">union<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">3</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'c'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token number">1</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'b'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$union</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [1 => ['a'], 2 => ['b'], 3 => ['c']]</span></code></pre><p><a name="method-unique"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">unique<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">unique</code> 方法返回集合中所有唯一的项目。返回的集合保留着原始键,所以在这个例子中我们用 <a href="#method-values"><code class=" language-php">values</code></a> 方法来把键重置为连续数字的键。</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$unique</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">unique<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$unique</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">values<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [1, 2, 3, 4]</span></code></pre><p>当处理嵌套数组或对象的时候,你可以指定用来决定唯一性的键:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span>
<span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'iPhone 6'</span><span class="token punctuation">,</span> <span class="token string">'brand'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Apple'</span><span class="token punctuation">,</span> <span class="token string">'type'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'phone'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'iPhone 5'</span><span class="token punctuation">,</span> <span class="token string">'brand'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Apple'</span><span class="token punctuation">,</span> <span class="token string">'type'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'phone'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Apple Watch'</span><span class="token punctuation">,</span> <span class="token string">'brand'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Apple'</span><span class="token punctuation">,</span> <span class="token string">'type'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'watch'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Galaxy S6'</span><span class="token punctuation">,</span> <span class="token string">'brand'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Samsung'</span><span class="token punctuation">,</span> <span class="token string">'type'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'phone'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">'name'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Galaxy Gear'</span><span class="token punctuation">,</span> <span class="token string">'brand'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Samsung'</span><span class="token punctuation">,</span> <span class="token string">'type'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'watch'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$unique</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">unique<span class="token punctuation">(</span></span><span class="token string">'brand'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$unique</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">values<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">/*
[
['name' => 'iPhone 6', 'brand' => 'Apple', 'type' => 'phone'],
['name' => 'Galaxy S6', 'brand' => 'Samsung', 'type' => 'phone'],
]
*/</span></code></pre><p>你可以传入自己的回调函数来确定项目的唯一性:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$unique</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">unique<span class="token punctuation">(</span></span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$item</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token variable">$item</span><span class="token punctuation">[</span><span class="token string">'brand'</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token variable">$item</span><span class="token punctuation">[</span><span class="token string">'type'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$unique</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">values<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">/*
[
['name' => 'iPhone 6', 'brand' => 'Apple', 'type' => 'phone'],
['name' => 'Apple Watch', 'brand' => 'Apple', 'type' => 'watch'],
['name' => 'Galaxy S6', 'brand' => 'Samsung', 'type' => 'phone'],
['name' => 'Galaxy Gear', 'brand' => 'Samsung', 'type' => 'watch'],
]
*/</span></code></pre><p><a name="method-values"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">values<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>返回「键」重新被设为「连续整数」的新集合:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span>
<span class="token number">10</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'product'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Desk'</span><span class="token punctuation">,</span> <span class="token string">'price'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">200</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token number">11</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token punctuation">[</span><span class="token string">'product'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Desk'</span><span class="token punctuation">,</span> <span class="token string">'price'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">200</span><span class="token punctuation">]</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$values</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">values<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$values</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">/*
[
0 => ['product' => 'Desk', 'price' => 200],
1 => ['product' => 'Desk', 'price' => 200],
]
*/</span></code></pre><p><a name="method-when"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">when<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>当第一个参数运算结果为 <code class=" language-php"><span class="token boolean">true</span></code> 的时候,会执行第二个参数传入的闭包:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">when<span class="token punctuation">(</span></span><span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$collection</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">return</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">push<span class="token punctuation">(</span></span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [1, 2, 3, 4]</span></code></pre><p><a name="method-where"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">where<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>以一对指定的「键/数值」筛选集合:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span>
<span class="token punctuation">[</span><span class="token string">'product'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Desk'</span><span class="token punctuation">,</span> <span class="token string">'price'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">200</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">'product'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Chair'</span><span class="token punctuation">,</span> <span class="token string">'price'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">'product'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Bookcase'</span><span class="token punctuation">,</span> <span class="token string">'price'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">150</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">'product'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Door'</span><span class="token punctuation">,</span> <span class="token string">'price'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$filtered</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">where<span class="token punctuation">(</span></span><span class="token string">'price'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$filtered</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">/*
[
['product' => 'Chair', 'price' => 100],
['product' => 'Door', 'price' => 100],
]
*/</span></code></pre><p>比较数值的时候用了「宽松」匹配方式,查看 <a href="#method-wherestrict"><code class=" language-php">whereStrict</code></a> method来用严格比较的方式过滤。</p><p><a name="method-wherestrict"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">whereStrict<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>这个方法与 <a href="#method-where"><code class=" language-php">where</code></a> 方法有着一样的形式;但是会以「严格」匹配来匹配数值:</p><p><a name="method-wherein"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">whereIn<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>基于参数中的键值数组进行过滤:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span>
<span class="token punctuation">[</span><span class="token string">'product'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Desk'</span><span class="token punctuation">,</span> <span class="token string">'price'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">200</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">'product'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Chair'</span><span class="token punctuation">,</span> <span class="token string">'price'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">'product'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Bookcase'</span><span class="token punctuation">,</span> <span class="token string">'price'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">150</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">[</span><span class="token string">'product'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token string">'Door'</span><span class="token punctuation">,</span> <span class="token string">'price'</span> <span class="token operator">=</span><span class="token operator">></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$filtered</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">whereIn<span class="token punctuation">(</span></span><span class="token string">'price'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">150</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$filtered</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">/*
[
['product' => 'Bookcase', 'price' => 150],
['product' => 'Desk', 'price' => 200],
]
*/</span></code></pre><p>此方法是用宽松的匹配,你可以使用 <a href="#method-whereinstrict"><code class=" language-php">whereInStrict</code></a> 做比较 <code class=" language-php">严格</code> 的匹配。</p><p><a name="method-whereinstrict"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">whereInStrict<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p>此方法的使用于 <a href="#method-wherein"><code class=" language-php">whereIn</code></a> 方法类似,只是使用了比较 <code class=" language-php">严格</code> 的过滤。</p><p><a name="method-zip"></a></p><h4 id="collection-method"><code class=" language-php"><span class="token function">zip<span class="token punctuation">(</span></span><span class="token punctuation">)</span></code></h4><p><code class=" language-php">zip</code> 方法将集合与指定数组相同索引的值合并在一起:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token string">'Chair'</span><span class="token punctuation">,</span> <span class="token string">'Desk'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$zipped</span> <span class="token operator">=</span> <span class="token variable">$collection</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">zip<span class="token punctuation">(</span></span><span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$zipped</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">all<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment" spellcheck="true">
// [['Chair', 100], ['Desk', 200]]</span></code></pre><p><a name="higher-order-messages"></a></p><h2><a href="#higher-order-messages">高阶信息传递</a></h2><p>集合也提供「高阶信息传递支持」,这是对集合执行常见操作的快捷方式。支持高阶信息传递的集合方法有: <code class=" language-php">contains</code>, <code class=" language-php">each</code>, <code class=" language-php">every</code>, <code class=" language-php">filter</code>, <code class=" language-php">first</code>, <code class=" language-php">map</code>, <code class=" language-php">partition</code>, <code class=" language-php">reject</code>, <code class=" language-php">sortBy</code>, <code class=" language-php">sortByDesc</code> 和 <code class=" language-php">sum</code>。</p><p>每个高阶信息都能作为集合实例的动态属性来访问。例如,我们在集合中使用 <code class=" language-php">each</code> 高阶信息传递方法拉哎对每个对象去调用一个方法:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token scope">User<span class="token punctuation">::</span></span><span class="token function">where<span class="token punctuation">(</span></span><span class="token string">'votes'</span><span class="token punctuation">,</span> <span class="token string">'>'</span><span class="token punctuation">,</span> <span class="token number">500</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">get<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$users</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">each</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">markAsVip<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre><p>同样,我们可以使用 <code class=" language-php">sum</code> 高阶信息传递的方式来统计出集合中用户总共的「投票数」:</p><pre class=" language-php"><code class=" language-php"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token scope">User<span class="token punctuation">::</span></span><span class="token function">where<span class="token punctuation">(</span></span><span class="token string">'group'</span><span class="token punctuation">,</span> <span class="token string">'Development'</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">get<span class="token punctuation">(</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> <span class="token variable">$users</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">sum</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">votes</span><span class="token punctuation">;</span></code></pre></article>
- 入门指南
- 安装
- 配置信息
- 文件夹结构
- 请求周期
- 开发环境部署
- Valet
- Homestead
- 核心概念
- 服务提供者
- Facades
- Contracts
- 服务容器
- HTTP 层
- 路由
- 中间件
- CSRF 保护
- 控制器
- 请求
- 响应
- 视图
- Session
- 表单验证
- 前端
- Blade 模板
- 本地化
- 前端指南
- 编辑资源 Mix
- 安全
- API 认证
- 用户认证
- 用户授权
- 加密解密
- 哈希
- 重置密码
- 数据库
- 快速入门
- 查询构造器
- 分页
- 数据库迁移
- Redis
- 数据填充
- Eloquent ORM
- Eloquent ORM快速入门
- 模型关联
- Eloquent 集合
- 修改器
- 序列化
- 综合话题
- Artisan 命令行
- 广播系统
- 缓存系统
- 集合
- 错误与日志
- 事件系统
- 文件存储
- 辅助函数
- 邮件发送
- 消息通知
- 扩展包开发
- 队列
- 任务调度