<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 集合 - [簡介](#introduction) - [創建集合](#creating-collections) - [可用的方法](#available-methods) <a name="introduction"></a> ## 簡介 `Illuminate\Support\Collection` 類提供一個流暢、便利的封裝來操控數組數據。如下面的示例代碼,我們用 `collect` 函數從數組中創建新的集合實例,對每一個元素運行 `strtoupper` 函數,然后移除所有的空元素: $collection = collect(['taylor', 'abigail', null])->map(function ($name) { return strtoupper($name); }) ->reject(function ($name) { return empty($name); }); 如上面的代碼示例,`Collection` 類支持鏈式調用,一般來說,每一個 `Collection` 方法會返回一個全新的 `Collection` 實例,你可以放心地進行鏈接調用。 <a name="creating-collections"></a> ### 創建集合 如上所述,`collect` 輔助函數會利用傳入的數組生成一個新的 `Illuminate\Support\Collection` 實例。所以要創建一個集合就這么簡單: $collection = collect([1, 2, 3]); > {tip} 默認 [Eloquent](/docs/{{version}}/eloquent) 模型的查詢結果總是以 `Collection` 實例返回。 <a name="available-methods"></a> ## 可用的方法 接下來,我們將會探討 `Collection` 類的所有方法。要記得的是,所有方法都支持鏈式調用,幾乎所有的方法都會返回新的 `Collection` 實例,讓你保留原版的集合以備不時之需。 <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" markdown="1"> [all](#method-all) [avg](#method-avg) [chunk](#method-chunk) [collapse](#method-collapse) [combine](#method-combine) [contains](#method-contains) [count](#method-count) [diff](#method-diff) [diffKeys](#method-diffkeys) [each](#method-each) [every](#method-every) [except](#method-except) [filter](#method-filter) [first](#method-first) [flatMap](#method-flatmap) [flatten](#method-flatten) [flip](#method-flip) [forget](#method-forget) [forPage](#method-forpage) [get](#method-get) [groupBy](#method-groupby) [has](#method-has) [implode](#method-implode) [intersect](#method-intersect) [isEmpty](#method-isempty) [keyBy](#method-keyby) [keys](#method-keys) [last](#method-last) [map](#method-map) [mapWithKeys](#method-mapwithkeys) [max](#method-max) [merge](#method-merge) [min](#method-min) [only](#method-only) [pipe](#method-pipe) [pluck](#method-pluck) [pop](#method-pop) [prepend](#method-prepend) [pull](#method-pull) [push](#method-push) [put](#method-put) [random](#method-random) [reduce](#method-reduce) [reject](#method-reject) [reverse](#method-reverse) [search](#method-search) [shift](#method-shift) [shuffle](#method-shuffle) [slice](#method-slice) [sort](#method-sort) [sortBy](#method-sortby) [sortByDesc](#method-sortbydesc) [splice](#method-splice) [sum](#method-sum) [take](#method-take) [toArray](#method-toarray) [toJson](#method-tojson) [transform](#method-transform) [union](#method-union) [unique](#method-unique) [values](#method-values) [where](#method-where) [whereStrict](#method-wherestrict) [whereIn](#method-wherein) [whereInLoose](#method-whereinloose) [zip](#method-zip) </div> <a name="method-listing"></a> ## 方法清單 <style> #collection-method code { font-size: 14px; } #collection-method:not(.first-collection-method) { margin-top: 50px; } </style> <a name="method-all"></a> #### `all()` {#collection-method .first-collection-method} 返回該集合所代表的底層 `數組`: collect([1, 2, 3])->all(); // [1, 2, 3] <a name="method-avg"></a> #### `avg()` {#collection-method} 返回集合中所有項目的平均值: collect([1, 2, 3, 4, 5])->avg(); // 3 如果集合包含了嵌套數組或對象,你可以通過傳遞「鍵」來指定使用哪些值計算平均值: $collection = collect([ ['name' => 'JavaScript: The Good Parts', 'pages' => 176], ['name' => 'JavaScript: The Definitive Guide', 'pages' => 1096], ]); $collection->avg('pages'); // 636 <a name="method-chunk"></a> #### `chunk()` {#collection-method} 將集合拆成多個指定大小的較小集合: $collection = collect([1, 2, 3, 4, 5, 6, 7]); $chunks = $collection->chunk(4); $chunks->toArray(); // [[1, 2, 3, 4], [5, 6, 7]] 這個方法在適用于網格系統如 [Bootstrap](http://getbootstrap.com/css/#grid) 的 [視圖](/docs/{{version}}/views) 。想像你有一個 [Eloquent](/docs/{{version}}/eloquent) 模型的集合要顯示在一個網格內: @foreach ($products->chunk(3) as $chunk) <div class="row"> @foreach ($chunk as $product) <div class="col-xs-4">{{ $product->name }}</div> @endforeach </div> @endforeach <a name="method-collapse"></a> #### `collapse()` {#collection-method} 將多個數組組成的集合合成單個一維數組集合: $collection = collect([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); $collapsed = $collection->collapse(); $collapsed->all(); // [1, 2, 3, 4, 5, 6, 7, 8, 9] <a name="method-combine"></a> #### `combine()` {#collection-method} 將集合的值作為「鍵」,合并另一個數組或者集合作為「鍵」對應的值。 $collection = collect(['name', 'age']); $combined = $collection->combine(['George', 29]); $combined->all(); // ['name' => 'George', 'age' => 29] <a name="method-contains"></a> #### `contains()` {#collection-method} 判斷集合是否含有指定項目: $collection = collect(['name' => 'Desk', 'price' => 100]); $collection->contains('Desk'); // true $collection->contains('New York'); // false 你可以將一對鍵/值傳入 `contains` 方法,用來判斷該組合是否存在于集合內: $collection = collect([ ['product' => 'Desk', 'price' => 200], ['product' => 'Chair', 'price' => 100], ]); $collection->contains('product', 'Bookcase'); // false 最后,你也可以傳入一個回調函數到 `contains` 方法內運行你自己的判斷語句: $collection = collect([1, 2, 3, 4, 5]); $collection->contains(function ($value, $key) { return $value > 5; }); // false <a name="method-count"></a> #### `count()` {#collection-method} 返回該集合內的項目總數: $collection = collect([1, 2, 3, 4]); $collection->count(); // 4 <a name="method-diff"></a> #### `diff()` {#collection-method} 將集合與其它集合或純 PHP `數組` 進行值的比較,返回第一個集合中存在而第二個集合中不存在的值: $collection = collect([1, 2, 3, 4, 5]); $diff = $collection->diff([2, 4, 6, 8]); $diff->all(); // [1, 3, 5] <a name="method-diffkeys"></a> #### `diffKeys()` {#collection-method} 將集合與其它集合或純 PHP `數組` 的「鍵」進行比較,返回第一個集合中存在而第二個集合中不存在「鍵」所對應的鍵值對: $collection = collect([ 'one' => 10, 'two' => 20, 'three' => 30, 'four' => 40, 'five' => 50, ]); $diff = $collection->diffKeys([ 'two' => 2, 'four' => 4, 'six' => 6, 'eight' => 8, ]); $diff->all(); // ['one' => 10, 'three' => 30, 'five' => 50] <a name="method-each"></a> #### `each()` {#collection-method} 遍歷集合中的項目,并將之傳入回調函數: $collection = $collection->each(function ($item, $key) { // }); 回調函數中返回 `false` 以中斷循環: $collection = $collection->each(function ($item, $key) { if (/* some condition */) { return false; } }); <a name="method-every"></a> #### `every()` {#collection-method} 創建一個包含每 **第 n 個** 元素的新集合: $collection = collect(['a', 'b', 'c', 'd', 'e', 'f']); $collection->every(4); // ['a', 'e'] 你可以選擇性的傳遞偏移值作為第二個參數: $collection->every(4, 1); // ['b', 'f'] <a name="method-except"></a> #### `except()` {#collection-method} 返回集合中除了指定鍵以外的所有項目: $collection = collect(['product_id' => 1, 'price' => 100, 'discount' => false]); $filtered = $collection->except(['price', 'discount']); $filtered->all(); // ['product_id' => 1] 與 `except` 相反的方法請查看 [only](#method-only)。 <a name="method-filter"></a> #### `filter()` {#collection-method} 使用回調函數篩選集合,只留下那些通過判斷測試的項目: $collection = collect([1, 2, 3, 4]); $filtered = $collection->filter(function ($value, $key) { return $value > 2; }); $filtered->all(); // [3, 4] 與 `filter` 相反的方法可以查看 [reject](#method-reject)。 <a name="method-first"></a> #### `first()` {#collection-method} 返回集合第一個通過指定測試的元素: collect([1, 2, 3, 4])->first(function ($value, $key) { return $value > 2; }); // 3 你也可以不傳入參數使用 `first` 方法以獲取集合中第一個元素。如果集合是空的,則會返回 `null`: collect([1, 2, 3, 4])->first(); // 1 <a name="method-flatmap"></a> #### `flatMap()` {#collection-method} 對集合內所有子集遍歷執行回調,并在最后轉為一維集合: $collection = collect([ ['name' => 'Sally'], ['school' => 'Arkansas'], ['age' => 28] ]); $flattened = $collection->flatMap(function ($values) { return array_map('strtoupper', $values); }); $flattened->all(); // ['name' => 'SALLY', 'school' => 'ARKANSAS', 'age' => '28']; <a name="method-flatten"></a> #### `flatten()` {#collection-method} 將多維集合轉為一維集合: $collection = collect(['name' => 'taylor', 'languages' => ['php', 'javascript']]); $flattened = $collection->flatten(); $flattened->all(); // ['taylor', 'php', 'javascript']; 你可以選擇性地傳入遍歷深度的參數: $collection = collect([ 'Apple' => [ ['name' => 'iPhone 6S', 'brand' => 'Apple'], ], 'Samsung' => [ ['name' => 'Galaxy S7', 'brand' => 'Samsung'] ], ]); $products = $collection->flatten(1); $products->values()->all(); /* [ ['name' => 'iPhone 6S', 'brand' => 'Apple'], ['name' => 'Galaxy S7', 'brand' => 'Samsung'], ] */ 在這個例子里,調用 `flatten` 方法時不傳入深度參數會遍歷嵌套數組降維成一維數組,生成 `['iPhone 6S', 'Apple', 'Galaxy S7', 'Samsung']`,傳入深度參數能讓你限制降維嵌套數組的層數。 <a name="method-flip"></a> #### `flip()` {#collection-method} 將集合中的鍵和對應的數值進行互換: $collection = collect(['name' => 'taylor', 'framework' => 'laravel']); $flipped = $collection->flip(); $flipped->all(); // ['taylor' => 'name', 'laravel' => 'framework'] <a name="method-forget"></a> #### `forget()` {#collection-method} 通過集合的鍵來移除掉集合中的一個項目: $collection = collect(['name' => 'taylor', 'framework' => 'laravel']); $collection->forget('name'); $collection->all(); // ['framework' => 'laravel'] > {note} 與大多數其它集合的方法不同,`forget` 不會返回修改過后的新集合;它會直接修改調用它的集合。 <a name="method-forpage"></a> #### `forPage()` {#collection-method} 返回可用來在指定頁碼上所顯示項目的新集合。這個方法第一個參數是頁碼數,第二個參數是每頁顯示的個數。 $collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9]); $chunk = $collection->forPage(2, 3); $chunk->all(); // [4, 5, 6] <a name="method-get"></a> #### `get()` {#collection-method} 返回指定鍵的項目。如果該鍵不存在,則返回 `null`: $collection = collect(['name' => 'taylor', 'framework' => 'laravel']); $value = $collection->get('name'); // taylor 你可以選擇性地傳入一個默認值作為第二個參數: $collection = collect(['name' => 'taylor', 'framework' => 'laravel']); $value = $collection->get('foo', 'default-value'); // default-value 你甚至可以傳入回調函數當默認值。如果指定的鍵不存在,就會返回回調函數的運行結果: $collection->get('email', function () { return 'default-value'; }); // default-value <a name="method-groupby"></a> #### `groupBy()` {#collection-method} 根據指定的「鍵」為集合內的項目分組: $collection = collect([ ['account_id' => 'account-x10', 'product' => 'Chair'], ['account_id' => 'account-x10', 'product' => 'Bookcase'], ['account_id' => 'account-x11', 'product' => 'Desk'], ]); $grouped = $collection->groupBy('account_id'); $grouped->toArray(); /* [ 'account-x10' => [ ['account_id' => 'account-x10', 'product' => 'Chair'], ['account_id' => 'account-x10', 'product' => 'Bookcase'], ], 'account-x11' => [ ['account_id' => 'account-x11', 'product' => 'Desk'], ], ] */ 除了傳入字符串的「鍵」之外,你也可以傳入回調函數。該函數應該返回你希望用來分組的鍵的值。 $grouped = $collection->groupBy(function ($item, $key) { return substr($item['account_id'], -3); }); $grouped->toArray(); /* [ 'x10' => [ ['account_id' => 'account-x10', 'product' => 'Chair'], ['account_id' => 'account-x10', 'product' => 'Bookcase'], ], 'x11' => [ ['account_id' => 'account-x11', 'product' => 'Desk'], ], ] */ <a name="method-has"></a> #### `has()` {#collection-method} 檢查集合中是否含有指定的「鍵」: $collection = collect(['account_id' => 1, 'product' => 'Desk']); $collection->has('email'); // false <a name="method-implode"></a> #### `implode()` {#collection-method} `implode` 方法合并集合中的項目。它的參數依集合中的項目類型而定。假如集合含有數組或對象,你應該傳入你希望連接的屬性的「鍵」,以及你希望放在數值之間的拼接字符串: $collection = collect([ ['account_id' => 1, 'product' => 'Desk'], ['account_id' => 2, 'product' => 'Chair'], ]); $collection->implode('product', ', '); // Desk, Chair 假如集合只含有簡單的字符串或數字,則只需要傳入拼接的字符串作為該方法的唯一參數即可: collect([1, 2, 3, 4, 5])->implode('-'); // '1-2-3-4-5' <a name="method-intersect"></a> #### `intersect()` {#collection-method} 移除任何指定 `數組` 或集合內所沒有的數值。最終集合保存著原集合的鍵: $collection = collect(['Desk', 'Sofa', 'Chair']); $intersect = $collection->intersect(['Desk', 'Chair', 'Bookcase']); $intersect->all(); // [0 => 'Desk', 2 => 'Chair'] <a name="method-isempty"></a> #### `isEmpty()` {#collection-method} 如果集合是空的,`isEmpty` 方法會返回 `true`:否則返回 `false`: collect([])->isEmpty(); // true <a name="method-keyby"></a> #### `keyBy()` {#collection-method} 以指定鍵的值作為集合項目的鍵。如果幾個數據項有相同的鍵,那在新集合中只顯示最后一項: $collection = collect([ ['product_id' => 'prod-100', 'name' => 'desk'], ['product_id' => 'prod-200', 'name' => 'chair'], ]); $keyed = $collection->keyBy('product_id'); $keyed->all(); /* [ 'prod-100' => ['product_id' => 'prod-100', 'name' => 'Desk'], 'prod-200' => ['product_id' => 'prod-200', 'name' => 'Chair'], ] */ 你也可以傳入自己的回調函數,該函數應該返回集合的鍵的值: $keyed = $collection->keyBy(function ($item) { return strtoupper($item['product_id']); }); $keyed->all(); /* [ 'PROD-100' => ['product_id' => 'prod-100', 'name' => 'Desk'], 'PROD-200' => ['product_id' => 'prod-200', 'name' => 'Chair'], ] */ <a name="method-keys"></a> #### `keys()` {#collection-method} 返回該集合所有的鍵: $collection = collect([ 'prod-100' => ['product_id' => 'prod-100', 'name' => 'Desk'], 'prod-200' => ['product_id' => 'prod-200', 'name' => 'Chair'], ]); $keys = $collection->keys(); $keys->all(); // ['prod-100', 'prod-200'] <a name="method-last"></a> #### `last()` {#collection-method} 返回集合中,最后一個通過指定測試的元素: collect([1, 2, 3, 4])->last(function ($value, $key) { return $value < 3; }); // 2 你也可以不傳入參數使用 `last` 方法以獲取集合中最后一個元素。如果集合是空的,則會返回 `null`: collect([1, 2, 3, 4])->last(); // 4 <a name="method-map"></a> #### `map()` {#collection-method} 遍歷整個集合并將每一個數值傳入回調函數。回調函數可以任意修改并返回項目,形成修改過的項目組成的新集合: $collection = collect([1, 2, 3, 4, 5]); $multiplied = $collection->map(function ($item, $key) { return $item * 2; }); $multiplied->all(); // [2, 4, 6, 8, 10] > {note} 正如集合大多數其它的方法一樣,`map` 返回一個新集合實例;它并沒有修改被調用的集合。假如你想改變原始的集合,得使用 [`transform`](#method-transform) 方法。 <a name="method-mapwithkeys"></a> #### `mapWithKeys()` {#collection-method} 遍歷整個集合并將每一個數值傳入回調函數。回調函數返回包含一個鍵值對的關聯數組: $collection = collect([ [ 'name' => 'John', 'department' => 'Sales', 'email' => 'john@example.com' ], [ 'name' => 'Jane', 'department' => 'Marketing', 'email' => 'jane@example.com' ] ]); $keyed = $collection->mapWithKeys(function ($item) { return [$item['email'] => $item['name']]; }); $keyed->all(); /* [ 'john@example.com' => 'John', 'jane@example.com' => 'Jane', ] */ <a name="method-max"></a> #### `max()` {#collection-method} 計算指定鍵的最大值: $max = collect([['foo' => 10], ['foo' => 20]])->max('foo'); // 20 $max = collect([1, 2, 3, 4, 5])->max(); // 5 <a name="method-merge"></a> #### `merge()` {#collection-method} 合并數組進集合。數組「鍵」對應的數值會覆蓋集合「鍵」對應的數值: $collection = collect(['product_id' => 1, 'price' => 100]); $merged = $collection->merge(['price' => 200, 'discount' => false]); $merged->all(); // ['product_id' => 1, 'price' => 200, 'discount' => false] 如果指定數組的「鍵」為數字,則「值」將會合并到集合的后面: $collection = collect(['Desk', 'Chair']); $merged = $collection->merge(['Bookcase', 'Door']); $merged->all(); // ['Desk', 'Chair', 'Bookcase', 'Door'] <a name="method-min"></a> #### `min()` {#collection-method} 計算指定「鍵」的最小值: $min = collect([['foo' => 10], ['foo' => 20]])->min('foo'); // 10 $min = collect([1, 2, 3, 4, 5])->min(); // 1 <a name="method-only"></a> #### `only()` {#collection-method} 返回集合中指定鍵的所有項目: $collection = collect(['product_id' => 1, 'name' => 'Desk', 'price' => 100, 'discount' => false]); $filtered = $collection->only(['product_id', 'name']); $filtered->all(); // ['product_id' => 1, 'name' => 'Desk'] 與 `only` 相反的方法請查看 [except](#method-only)。 <a name="method-pipe"></a> #### `pipe()` {#collection-method} 將集合傳給回調函數并返回結果: $collection = collect([1, 2, 3]); $piped = $collection->pipe(function ($collection) { return $collection->sum(); }); // 6 <a name="method-pluck"></a> #### `pluck()` {#collection-method} 獲取集合中指定「鍵」所有對應的值: $collection = collect([ ['product_id' => 'prod-100', 'name' => 'Desk'], ['product_id' => 'prod-200', 'name' => 'Chair'], ]); $plucked = $collection->pluck('name'); $plucked->all(); // ['Desk', 'Chair'] 你也可以指定最終集合的鍵: $plucked = $collection->pluck('name', 'product_id'); $plucked->all(); // ['prod-100' => 'Desk', 'prod-200' => 'Chair'] <a name="method-pop"></a> #### `pop()` {#collection-method} 移除并返回集合最后一個項目: $collection = collect([1, 2, 3, 4, 5]); $collection->pop(); // 5 $collection->all(); // [1, 2, 3, 4] <a name="method-prepend"></a> #### `prepend()` {#collection-method} 在集合前面增加一項數組的值: $collection = collect([1, 2, 3, 4, 5]); $collection->prepend(0); $collection->all(); // [0, 1, 2, 3, 4, 5] 你可以傳遞第二個參數來設置新增加項的鍵: $collection = collect(['one' => 1, 'two' => 2]); $collection->prepend(0, 'zero'); $collection->all(); // ['zero' => 0, 'one' => 1, 'two', => 2] <a name="method-pull"></a> #### `pull()` {#collection-method} 把「鍵」對應的值從集合中移除并返回: $collection = collect(['product_id' => 'prod-100', 'name' => 'Desk']); $collection->pull('name'); // 'Desk' $collection->all(); // ['product_id' => 'prod-100'] <a name="method-push"></a> #### `push()` {#collection-method} 在集合的后面新添加一個元素: $collection = collect([1, 2, 3, 4]); $collection->push(5); $collection->all(); // [1, 2, 3, 4, 5] <a name="method-put"></a> #### `put()` {#collection-method} 在集合內設置一個「鍵/值」: $collection = collect(['product_id' => 1, 'name' => 'Desk']); $collection->put('price', 100); $collection->all(); // ['product_id' => 1, 'name' => 'Desk', 'price' => 100] <a name="method-random"></a> #### `random()` {#collection-method} `random` 方法從集合中隨機返回一個項目: $collection = collect([1, 2, 3, 4, 5]); $collection->random(); // 4 - (retrieved randomly) 你可以選擇性地傳入一個整數到 `random`。如果該整數大于 `1`,則會返回一個集合: $random = $collection->random(3); $random->all(); // [2, 4, 5] - (retrieved randomly) <a name="method-reduce"></a> #### `reduce()` {#collection-method} `reduce` 方法將集合縮減到單個數值,該方法會將每次迭代的結果傳入到下一次迭代: $collection = collect([1, 2, 3]); $total = $collection->reduce(function ($carry, $item) { return $carry + $item; }); // 6 第一次迭代時 `$carry` 的數值為 `null`;然而你也可以傳入第二個參數進 `reduce` 以指定它的初始值: $collection->reduce(function ($carry, $item) { return $carry + $item; }, 4); // 10 <a name="method-reject"></a> #### `reject()` {#collection-method} `reject` 方法以指定的回調函數篩選集合。該回調函數應該對希望從最終集合移除掉的項目返回 `true`: $collection = collect([1, 2, 3, 4]); $filtered = $collection->reject(function ($value, $key) { return $value > 2; }); $filtered->all(); // [1, 2] 與 `reject` 相反的方法可以查看 [`filter`](#method-filter) 方法。 <a name="method-reverse"></a> #### `reverse()` {#collection-method} `reverse` 方法倒轉集合內項目的順序: $collection = collect([1, 2, 3, 4, 5]); $reversed = $collection->reverse(); $reversed->all(); // [5, 4, 3, 2, 1] <a name="method-search"></a> #### `search()` {#collection-method} `search` 方法在集合內搜索指定的數值并返回找到的鍵。假如找不到項目,則返回 `false`: $collection = collect([2, 4, 6, 8]); $collection->search(4); // 1 搜索是用「寬松」匹配來進行,也就是說如果字符串值是整數那它就跟這個整數是相等的。要使用嚴格匹配的話,就傳入 `true` 為該方法的第二個參數: $collection->search('4', true); // false 另外,你可以傳入你自己的回調函數來搜索第一個通過你判斷測試的項目: $collection->search(function ($item, $key) { return $item > 5; }); // 2 <a name="method-shift"></a> #### `shift()` {#collection-method} `shift` 方法移除并返回集合的第一個項目: $collection = collect([1, 2, 3, 4, 5]); $collection->shift(); // 1 $collection->all(); // [2, 3, 4, 5] <a name="method-shuffle"></a> #### `shuffle()` {#collection-method} `shuffle` 方法隨機排序集合的項目: $collection = collect([1, 2, 3, 4, 5]); $shuffled = $collection->shuffle(); $shuffled->all(); // [3, 2, 5, 1, 4] // (generated randomly) <a name="method-slice"></a> #### `slice()` {#collection-method} `slice` 方法返回集合從指定索引開始的一部分切片: $collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); $slice = $collection->slice(4); $slice->all(); // [5, 6, 7, 8, 9, 10] 如果你想限制返回切片的大小,就傳入想要的大小為方法的第二個參數: $slice = $collection->slice(4, 2); $slice->all(); // [5, 6] 返回的切片將會保留原始鍵作為索引。假如你不希望保留原始的鍵,你可以使用 `values` 方法來重新建立索引。 <a name="method-sort"></a> #### `sort()` {#collection-method} 對集合排序。排序后的集合保留著原始數組的鍵,所以在這個例子里我們用 [`values`](#method-values) 方法來把鍵設置為連續數字的鍵。 $collection = collect([5, 3, 1, 2, 4]); $sorted = $collection->sort(); $sorted->values()->all(); // [1, 2, 3, 4, 5] 假如你需要更高級的排序,你可以傳入回調函數以你自己的算法進行`排序`。參考 PHP 文檔的 [`usort`](http://php.net/manual/en/function.usort.php#refsect1-function.usort-parameters),這是集合的 `sort` 方法在背后所調用的函數。 > {tip} 要排序嵌套數組或對象的集合,見 [`sortBy`](#method-sortby) 和 [`sortByDesc`](#method-sortbydesc) 方法。 <a name="method-sortby"></a> #### `sortBy()` {#collection-method} 以指定的鍵排序集合。排序后的集合保留了原始數組鍵,所以在這個例子中我們用 [`values`](#method-values) method 把鍵設置為連續數字的索引建: $collection = collect([ ['name' => 'Desk', 'price' => 200], ['name' => 'Chair', 'price' => 100], ['name' => 'Bookcase', 'price' => 150], ]); $sorted = $collection->sortBy('price'); $sorted->values()->all(); /* [ ['name' => 'Chair', 'price' => 100], ['name' => 'Bookcase', 'price' => 150], ['name' => 'Desk', 'price' => 200], ] */ 你也可以傳入自己的回調函數以決定如何排序集合數值: $collection = collect([ ['name' => 'Desk', 'colors' => ['Black', 'Mahogany']], ['name' => 'Chair', 'colors' => ['Black']], ['name' => 'Bookcase', 'colors' => ['Red', 'Beige', 'Brown']], ]); $sorted = $collection->sortBy(function ($product, $key) { return count($product['colors']); }); $sorted->values()->all(); /* [ ['name' => 'Chair', 'colors' => ['Black']], ['name' => 'Desk', 'colors' => ['Black', 'Mahogany']], ['name' => 'Bookcase', 'colors' => ['Red', 'Beige', 'Brown']], ] */ <a name="method-sortbydesc"></a> #### `sortByDesc()` {#collection-method} 與 [`sortBy`](#method-sortby) 有著一樣的形式,但是會以相反的順序來排序集合: <a name="method-splice"></a> #### `splice()` {#collection-method} 返回從指定的索引開始的一小切片項目,原本集合也會被切除: $collection = collect([1, 2, 3, 4, 5]); $chunk = $collection->splice(2); $chunk->all(); // [3, 4, 5] $collection->all(); // [1, 2] 你可以傳入第二個參數以限制大小: $collection = collect([1, 2, 3, 4, 5]); $chunk = $collection->splice(2, 1); $chunk->all(); // [3] $collection->all(); // [1, 2, 4, 5] 此外,你可以傳入含有新項目的第三個參數以取代集合中被移除的項目: $collection = collect([1, 2, 3, 4, 5]); $chunk = $collection->splice(2, 1, [10, 11]); $chunk->all(); // [3] $collection->all(); // [1, 2, 10, 11, 4, 5] <a name="method-sum"></a> #### `sum()` {#collection-method} 返回集合內所有項目的總和: collect([1, 2, 3, 4, 5])->sum(); // 15 如果集合包含嵌套數組或對象,你應該傳入一個「鍵」來指定要用哪些數值來計算總合: $collection = collect([ ['name' => 'JavaScript: The Good Parts', 'pages' => 176], ['name' => 'JavaScript: The Definitive Guide', 'pages' => 1096], ]); $collection->sum('pages'); // 1272 此外,你可以傳入自己的回調函數來決定要用哪些數值來計算總合: $collection = collect([ ['name' => 'Chair', 'colors' => ['Black']], ['name' => 'Desk', 'colors' => ['Black', 'Mahogany']], ['name' => 'Bookcase', 'colors' => ['Red', 'Beige', 'Brown']], ]); $collection->sum(function ($product) { return count($product['colors']); }); // 6 <a name="method-take"></a> #### `take()` {#collection-method} 返回有著指定數量項目的集合: $collection = collect([0, 1, 2, 3, 4, 5]); $chunk = $collection->take(3); $chunk->all(); // [0, 1, 2] 你也可以傳入負整數以獲取從集合后面來算指定數量的項目: $collection = collect([0, 1, 2, 3, 4, 5]); $chunk = $collection->take(-2); $chunk->all(); // [4, 5] <a name="method-toarray"></a> #### `toArray()` {#collection-method} 將集合轉換成純 PHP `數組`。假如集合的數值是 [Eloquent](/docs/{{version}}/eloquent) 模型,也會被轉換成數組: $collection = collect(['name' => 'Desk', 'price' => 200]); $collection->toArray(); /* [ ['name' => 'Desk', 'price' => 200], ] */ > {note} `toArray` 也會轉換所有內嵌的對象為數組。假如你希望獲取原本的底層數組,改用 [`all`](#method-all) 方法。 <a name="method-tojson"></a> #### `toJson()` {#collection-method} 將集合轉換成 JSON: $collection = collect(['name' => 'Desk', 'price' => 200]); $collection->toJson(); // '{"name":"Desk", "price":200}' <a name="method-transform"></a> #### `transform()` {#collection-method} 遍歷集合并對集合內每一個項目調用指定的回調函數。集合的項目將會被回調函數返回的數值取代掉: $collection = collect([1, 2, 3, 4, 5]); $collection->transform(function ($item, $key) { return $item * 2; }); $collection->all(); // [2, 4, 6, 8, 10] > {note} 與大多數其它集合的方法不同,`transform` 會修改集合本身。如果你希望創建新集合,就改用 [`map`](#method-map) 方法。 <a name="method-union"></a> #### `union()` {#collection-method} 將給定的數組合并到集合中,如果數組中含有與集合一樣的「鍵」,集合的鍵值會被保留: $collection = collect([1 => ['a'], 2 => ['b']]); $union = $collection->union([3 => ['c'], 1 => ['b']]); $union->all(); // [1 => ['a'], 2 => ['b'], [3 => ['c']] <a name="method-unique"></a> #### `unique()` {#collection-method} `unique` 方法返回集合中所有唯一的項目。返回的集合保留著原始鍵,所以在這個例子中我們用 [`values`](#method-values) 方法來把鍵重置為連續數字的鍵。 $collection = collect([1, 1, 2, 2, 3, 4, 2]); $unique = $collection->unique(); $unique->values()->all(); // [1, 2, 3, 4] 當處理嵌套數組或對象的時候,你可以指定用來決定唯一性的鍵: $collection = collect([ ['name' => 'iPhone 6', 'brand' => 'Apple', 'type' => 'phone'], ['name' => 'iPhone 5', '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'], ]); $unique = $collection->unique('brand'); $unique->values()->all(); /* [ ['name' => 'iPhone 6', 'brand' => 'Apple', 'type' => 'phone'], ['name' => 'Galaxy S6', 'brand' => 'Samsung', 'type' => 'phone'], ] */ 你可以傳入自己的回調函數來確定項目的唯一性: $unique = $collection->unique(function ($item) { return $item['brand'].$item['type']; }); $unique->values()->all(); /* [ ['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'], ] */ <a name="method-values"></a> #### `values()` {#collection-method} 返回「鍵」重新被設為「連續整數」的新集合: $collection = collect([ 10 => ['product' => 'Desk', 'price' => 200], 11 => ['product' => 'Desk', 'price' => 200] ]); $values = $collection->values(); $values->all(); /* [ 0 => ['product' => 'Desk', 'price' => 200], 1 => ['product' => 'Desk', 'price' => 200], ] */ <a name="method-where"></a> #### `where()` {#collection-method} 以一對指定的「鍵/數值」篩選集合: $collection = collect([ ['product' => 'Desk', 'price' => 200], ['product' => 'Chair', 'price' => 100], ['product' => 'Bookcase', 'price' => 150], ['product' => 'Door', 'price' => 100], ]); $filtered = $collection->where('price', 100); $filtered->all(); /* [ ['product' => 'Chair', 'price' => 100], ['product' => 'Door', 'price' => 100], ] */ 比較數值的時候用了「寬松」匹配方式,查看 [`whereStrict`](#method-wherestrict) method來用嚴格比較的方式過濾。 <a name="method-wherestrict"></a> #### `whereStrict()` {#collection-method} 這個方法與 [`where`](#method-where) 方法有著一樣的形式;但是會以「嚴格」匹配來匹配數值: <a name="method-wherein"></a> #### `whereIn()` {#collection-method} 基于參數中的鍵值數組進行過濾: $collection = collect([ ['product' => 'Desk', 'price' => 200], ['product' => 'Chair', 'price' => 100], ['product' => 'Bookcase', 'price' => 150], ['product' => 'Door', 'price' => 100], ]); $filtered = $collection->whereIn('price', [150, 200]); $filtered->all(); /* [ ['product' => 'Bookcase', 'price' => 150], ['product' => 'Desk', 'price' => 200], ] */ 此方法是用嚴格的匹配,你可以使用 [`whereInLoose`](#method-whereinloose) 做比較 `寬松` 的匹配。 <a name="method-whereinloose"></a> #### `whereInLoose()` {#collection-method} 此方法的使用于 [`whereIn`](#method-wherein) 方法類似,只是使用了比較 `寬松` 的過濾。 <a name="method-zip"></a> #### `zip()` {#collection-method} `zip` 方法將集合與指定數組相同索引的值合并在一起: $collection = collect(['Chair', 'Desk']); $zipped = $collection->zip([100, 200]); $zipped->all(); // [['Chair', 100], ['Desk', 200]]
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看