<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ### 可用方法 在本文檔的其余部分,我們將討論`Collection`該類上可用的每種方法。請記住,所有這些方法都可以鏈接在一起,以流暢地操作基礎數組。此外,幾乎每個方法都返回一個新`Collection`實例,使您可以在必要時保留集合的原始副本。 您可以從此表中選擇任何方法以查看其用法示例: [所有](https://octobercms.com/docs/services/collections#method-all)[平均](https://octobercms.com/docs/services/collections#method-average)[平均](https://octobercms.com/docs/services/collections#method-avg)[大塊](https://octobercms.com/docs/services/collections#method-chunk)[坍方](https://octobercms.com/docs/services/collections#method-collapse)[收藏](https://octobercms.com/docs/services/collections#method-collect)[結合](https://octobercms.com/docs/services/collections#method-combine)[康卡特](https://octobercms.com/docs/services/collections#method-concat)[包含](https://octobercms.com/docs/services/collections#method-contains)[containsStrict](https://octobercms.com/docs/services/collections#method-containsstrict)[計數](https://octobercms.com/docs/services/collections#method-count)[計數](https://octobercms.com/docs/services/collections#method-countBy)[交叉連接](https://octobercms.com/docs/services/collections#method-crossjoin)[dd](https://octobercms.com/docs/services/collections#method-dd)[差異](https://octobercms.com/docs/services/collections#method-diff)[diffAssoc](https://octobercms.com/docs/services/collections#method-diffassoc)[diffKeys](https://octobercms.com/docs/services/collections#method-diffkeys)[傾倒](https://octobercms.com/docs/services/collections#method-dump)[重復項](https://octobercms.com/docs/services/collections#method-duplicates)[嚴格重復](https://octobercms.com/docs/services/collections#method-duplicatesstrict)[每](https://octobercms.com/docs/services/collections#method-each)[過濾](https://octobercms.com/docs/services/collections#method-filter)[第一](https://octobercms.com/docs/services/collections#method-first)[首先在哪里](https://octobercms.com/docs/services/collections#method-first-where)[flatMap](https://octobercms.com/docs/services/collections#method-flatmap)[展平](https://octobercms.com/docs/services/collections#method-flatten)[翻轉](https://octobercms.com/docs/services/collections#method-flip)[忘記](https://octobercms.com/docs/services/collections#method-forget)[forPage](https://octobercms.com/docs/services/collections#method-forpage)[得到](https://octobercms.com/docs/services/collections#method-get)[通過...分組](https://octobercms.com/docs/services/collections#method-groupby)[具有](https://octobercms.com/docs/services/collections#method-has)[內爆](https://octobercms.com/docs/services/collections#method-implode)[相交](https://octobercms.com/docs/services/collections#method-intersect)[相交鍵](https://octobercms.com/docs/services/collections#method-intersectbykeys)[是空的](https://octobercms.com/docs/services/collections#method-isempty)[isNotEmpty](https://octobercms.com/docs/services/collections#method-isnotempty)[加入](https://octobercms.com/docs/services/collections#method-join)[關鍵](https://octobercms.com/docs/services/collections#method-keyby)[鍵](https://octobercms.com/docs/services/collections#method-keys)[持續](https://octobercms.com/docs/services/collections#method-last)[地圖](https://octobercms.com/docs/services/collections#method-map)[mapInto](https://octobercms.com/docs/services/collections#method-mapinto)[mapSpread](https://octobercms.com/docs/services/collections#method-mapspread)[mapToGroups](https://octobercms.com/docs/services/collections#method-maptogroups)[mapWithKeys](https://octobercms.com/docs/services/collections#method-mapwithkeys)[最高](https://octobercms.com/docs/services/collections#method-max)[中位數](https://octobercms.com/docs/services/collections#method-median)[合并](https://octobercms.com/docs/services/collections#method-merge)[mergeRecursive](https://octobercms.com/docs/services/collections#method-mergerecursive)[分](https://octobercms.com/docs/services/collections#method-min)[模式](https://octobercms.com/docs/services/collections#method-mode)[第n](https://octobercms.com/docs/services/collections#method-nth)[只要](https://octobercms.com/docs/services/collections#method-only)[墊](https://octobercms.com/docs/services/collections#method-pad)[劃分](https://octobercms.com/docs/services/collections#method-partition)[管](https://octobercms.com/docs/services/collections#method-pipe)[采摘](https://octobercms.com/docs/services/collections#method-pluck)[流行音樂](https://octobercms.com/docs/services/collections#method-pop)[前置](https://octobercms.com/docs/services/collections#method-prepend)[拉](https://octobercms.com/docs/services/collections#method-pull)[推](https://octobercms.com/docs/services/collections#method-push)[放](https://octobercms.com/docs/services/collections#method-put)[隨機](https://octobercms.com/docs/services/collections#method-random)[降低](https://octobercms.com/docs/services/collections#method-reduce)[拒絕](https://octobercms.com/docs/services/collections#method-reject)[更換](https://octobercms.com/docs/services/collections#method-replace)[replaceRecursive](https://octobercms.com/docs/services/collections#method-replacerecursive)[逆轉](https://octobercms.com/docs/services/collections#method-reverse)[搜索](https://octobercms.com/docs/services/collections#method-search)[轉移](https://octobercms.com/docs/services/collections#method-shift)[洗牌](https://octobercms.com/docs/services/collections#method-shuffle)[跳躍](https://octobercms.com/docs/services/collections#method-skip)[片](https://octobercms.com/docs/services/collections#method-slice)[一些](https://octobercms.com/docs/services/collections#method-some)[分類](https://octobercms.com/docs/services/collections#method-sort)[排序方式](https://octobercms.com/docs/services/collections#method-sortby)[sortByDesc](https://octobercms.com/docs/services/collections#method-sortbydesc)[sortKeys](https://octobercms.com/docs/services/collections#method-sortkeys)[sortKeysDesc](https://octobercms.com/docs/services/collections#method-sortkeysdesc)[拼接](https://octobercms.com/docs/services/collections#method-splice)[分裂](https://octobercms.com/docs/services/collections#method-split)[和](https://octobercms.com/docs/services/collections#method-sum)[采取](https://octobercms.com/docs/services/collections#method-take)[輕拍](https://octobercms.com/docs/services/collections#method-tap)[次](https://octobercms.com/docs/services/collections#method-times)[toArray](https://octobercms.com/docs/services/collections#method-toarray)[托森](https://octobercms.com/docs/services/collections#method-tojson)[轉變](https://octobercms.com/docs/services/collections#method-transform)[聯盟](https://octobercms.com/docs/services/collections#method-union)[獨特](https://octobercms.com/docs/services/collections#method-unique)[uniqueStrict](https://octobercms.com/docs/services/collections#method-uniquestrict)[除非](https://octobercms.com/docs/services/collections#method-unless)[除非空](https://octobercms.com/docs/services/collections#method-unlessempty)[除非NotEmpty](https://octobercms.com/docs/services/collections#method-unlessnotempty)[拆開](https://octobercms.com/docs/services/collections#method-unwrap)[價值觀](https://octobercms.com/docs/services/collections#method-values)[什么時候](https://octobercms.com/docs/services/collections#method-when)[whenEmpty](https://octobercms.com/docs/services/collections#method-whenempty)[whenNotEmpty](https://octobercms.com/docs/services/collections#method-whennotempty)[哪里](https://octobercms.com/docs/services/collections#method-where)[嚴格](https://octobercms.com/docs/services/collections#method-wherestrict)[之間](https://octobercms.com/docs/services/collections#method-wherebetween)[在哪里](https://octobercms.com/docs/services/collections#method-wherein)[whereInStrict](https://octobercms.com/docs/services/collections#method-whereinstrict)[whereInstanceOf](https://octobercms.com/docs/services/collections#method-whereinstanceof)[whereNotBetween](https://octobercms.com/docs/services/collections#method-wherenotbetween)[whereNotIn](https://octobercms.com/docs/services/collections#method-wherenotin)[WhereNotInStrict](https://octobercms.com/docs/services/collections#method-wherenotinstrict)[whereNotNull](https://octobercms.com/docs/services/collections#method-wherenotnull)[哪里空](https://octobercms.com/docs/services/collections#method-wherenull)[包](https://octobercms.com/docs/services/collections#method-wrap)[壓縮](https://octobercms.com/docs/services/collections#method-zip) ### [](https://octobercms.com/docs/services/collections#method-listing)方法清單 #### [](https://octobercms.com/docs/services/collections#method-all)`all()` 該`all`方法僅返回由集合表示的基礎數組: ~~~ $collection = new Collection([1, 2, 3]); $collection->all(); // [1, 2, 3] ~~~ #### [](https://octobercms.com/docs/services/collections#method-average)`average()` 該[`avg`](https://octobercms.com/docs/services/collections#method-avg)方法的別名。 #### [](https://octobercms.com/docs/services/collections#method-avg)`avg()` 該`avg`方法返回給定鍵的[平均值](https://en.wikipedia.org/wiki/Average): ~~~ $average = collect([['foo' => 10], ['foo' => 10], ['foo' => 20], ['foo' => 40]])->avg('foo'); // 20 $average = collect([1, 1, 2, 4])->avg(); // 2 ~~~ #### [](https://octobercms.com/docs/services/collections#method-chunk)`chunk()` 該`chunk`方法將集合分成給定大小的多個較小的集合: ~~~ $collection = new Collection([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)時,此方法在[CMS頁面中](https://octobercms.com/docs/cms/pages)特別有用。假設您要在網格中顯示一組模型:[](http://getbootstrap.com/css/#grid) ~~~ {% for chunk in products.chunk(3) %} <div class="row"> {% for product in chunk %} <div class="col-xs-4">{{ product.name }}</div> {% endfor %} </div> {% endfor %} ~~~ #### [](https://octobercms.com/docs/services/collections#method-collapse)`collapse()` 該`collapse`方法將數組的集合折疊為一個平面集合: ~~~ $collection = new Collection([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); $collapsed = $collection->collapse(); $collapsed->all(); // [1, 2, 3, 4, 5, 6, 7, 8, 9] ~~~ #### [](https://octobercms.com/docs/services/collections#method-combine)`combine()` 該`combine`方法將集合的值(作為鍵)與另一個數組或集合的值組合: ~~~ $collection = collect(['name', 'age']); $combined = $collection->combine(['George', 29]); $combined->all(); // ['name' => 'George', 'age' => 29] ~~~ #### [](https://octobercms.com/docs/services/collections#method-collect)`collect()` 該`collect`方法返回一個新`Collection`實例,其中包含當前集合中的項目: ~~~ $collectionA = collect([1, 2, 3]); $collectionB = $collectionA->collect(); $collectionB->all(); // [1, 2, 3] ~~~ 該`collect`方法主要用于將[惰性集合](https://octobercms.com/docs/services/collections#lazy-collections)轉換為標準`Collection`實例: ~~~ $lazyCollection = LazyCollection::make(function () { yield 1; yield 2; yield 3; }); $collection = $lazyCollection->collect(); get_class($collection); // 'Illuminate\Support\Collection' $collection->all(); // [1, 2, 3] ~~~ > **提示:**`collect`當您有一個實例`Enumerable`并且需要一個非延遲集合實例時,該方法特別有用。由于`collect()`是`Enumerable`合同的一部分,因此您可以放心地使用它來獲取`Collection`實例。 #### [](https://octobercms.com/docs/services/collections#method-concat)`concat()` 該`concat`方法將給定`array`或集合值附加到集合的末尾: ~~~ $collection = collect(['John Doe']); $concatenated = $collection->concat(['Jane Doe'])->concat(['name' => 'Johnny Doe']); $concatenated->all(); // ['John Doe', 'Jane Doe', 'Johnny Doe'] ~~~ #### [](https://octobercms.com/docs/services/collections#method-contains)`contains()` 該`contains`方法確定集合是否包含給定的項目: ~~~ $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 ~~~ 該`contains`方法在檢查項目值時使用“松散”比較,這意味著具有整數值的字符串將被視為等于相同值的整數。使用該[`containsStrict`](https://octobercms.com/docs/services/collections#method-containsstrict)方法使用“嚴格”比較進行過濾。 #### [](https://octobercms.com/docs/services/collections#method-containsstrict)`containsStrict()` 此方法與該方法具有相同的簽名[`contains`](https://octobercms.com/docs/services/collections#method-contains);但是,使用“嚴格”比較來比較所有值。 #### [](https://octobercms.com/docs/services/collections#method-count)`count()` 該`count`方法返回集合中的項目總數: ~~~ $collection = new Collection([1, 2, 3, 4]); $collection->count(); // 4 ~~~ #### [](https://octobercms.com/docs/services/collections#method-countBy)`countBy()` 該`countBy`方法計算集合中值的出現次數。默認情況下,該方法計算每個元素的出現次數: ~~~ $collection = collect([1, 2, 2, 2, 3]); $counted = $collection->countBy(); $counted->all(); // [1 => 1, 2 => 3, 3 => 1] ~~~ 但是,您將回調傳遞給該`countBy`方法以通過自定義值對所有項目進行計數: ~~~ $collection = collect(['alice@gmail.com', 'bob@yahoo.com', 'carlos@gmail.com']); $counted = $collection->countBy(function ($email) { return substr(strrchr($email, "@"), 1); }); $counted->all(); // ['gmail.com' => 2, 'yahoo.com' => 1] ~~~ #### [](https://octobercms.com/docs/services/collections#method-crossjoin)`crossJoin()` 該`crossJoin`方法將集合的值交叉連接到給定的數組或集合中,返回具有所有可能排列的笛卡爾乘積: ~~~ $collection = collect([1, 2]); $matrix = $collection->crossJoin(['a', 'b']); $matrix->all(); /* [ [1, 'a'], [1, 'b'], [2, 'a'], [2, 'b'], ] */ $collection = collect([1, 2]); $matrix = $collection->crossJoin(['a', 'b'], ['I', 'II']); $matrix->all(); /* [ [1, 'a', 'I'], [1, 'a', 'II'], [1, 'b', 'I'], [1, 'b', 'II'], [2, 'a', 'I'], [2, 'a', 'II'], [2, 'b', 'I'], [2, 'b', 'II'], ] */ ~~~ #### [](https://octobercms.com/docs/services/collections#method-dd)`dd()` 該`dd`方法轉儲集合的項目并結束腳本的執行: ~~~ $collection = collect(['John Doe', 'Jane Doe']); $collection->dd(); /* Collection { #items: array:2 [ 0 => "John Doe" 1 => "Jane Doe" ] } */ ~~~ 如果不想停止執行腳本,請改用[`dump`](https://octobercms.com/docs/services/collections#method-dump)方法。 #### [](https://octobercms.com/docs/services/collections#method-diff)`diff()` 該`diff`方法將集合與另一個集合或純PHP進行比較`array`: ~~~ $collection = new Collection([1, 2, 3, 4, 5]); $diff = $collection->diff([2, 4, 6, 8]); $diff->all(); // [1, 3, 5] ~~~ #### [](https://octobercms.com/docs/services/collections#method-diffassoc)`diffAssoc()` 該`diffAssoc`方法`array`根據其鍵和值將集合與另一個集合或純PHP進行比較。此方法將返回原始集合中給定集合中不存在的鍵/值對: ~~~ $collection = collect([ 'color' => 'orange', 'type' => 'fruit', 'remain' => 6 ]); $diff = $collection->diffAssoc([ 'color' => 'yellow', 'type' => 'fruit', 'remain' => 3, 'used' => 6, ]); $diff->all(); // ['color' => 'orange', 'remain' => 6] ~~~ #### [](https://octobercms.com/docs/services/collections#method-diffkeys)`diffKeys()` 該`diffKeys`方法根據集合`array`的鍵將集合與另一個集合或純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] ~~~ #### [](https://octobercms.com/docs/services/collections#method-dump)`dump()` 該`dump`方法轉儲集合的項目: ~~~ $collection = collect(['John Doe', 'Jane Doe']); $collection->dump(); /* Collection { #items: array:2 [ 0 => "John Doe" 1 => "Jane Doe" ] } */ ~~~ 如果要在轉儲集合后停止執行腳本,請改用[`dd`](https://octobercms.com/docs/services/collections#method-dd)方法。 #### [](https://octobercms.com/docs/services/collections#method-duplicates)`duplicates()` 該`duplicates`方法從集合中檢索并返回重復的值: ~~~ $collection = collect(['a', 'b', 'a', 'c', 'b']); $collection->duplicates(); // [2 => 'a', 4 => 'b'] ~~~ 如果集合包含數組或對象,則可以傳遞要檢查重復值的屬性的鍵: ~~~ $employees = collect([ ['email' => 'abigail@example.com', 'position' => 'Developer'], ['email' => 'james@example.com', 'position' => 'Designer'], ['email' => 'victoria@example.com', 'position' => 'Developer'], ]) $employees->duplicates('position'); // [2 => 'Developer'] ~~~ #### [](https://octobercms.com/docs/services/collections#method-duplicatesstrict)`duplicatesStrict()` 此方法與該方法具有相同的簽名[`duplicates`](https://octobercms.com/docs/services/collections#method-duplicates);但是,使用“嚴格”比較來比較所有值。 #### [](https://octobercms.com/docs/services/collections#method-each)`each()` 該`each`方法遍歷集合中的項目并將每個項目傳遞給回調: ~~~ $collection->each(function ($item, $key) { // }); ~~~ 如果您想停止遍歷所有項目,則可以`false`從回調中返回: ~~~ $collection->each(function ($item, $key) { if (/* some condition */) { return false; } }); ~~~ #### [](https://octobercms.com/docs/services/collections#method-every)`every()` 該`every`方法創建一個由第n個元素組成的新集合: ~~~ $collection = new Collection(['a', 'b', 'c', 'd', 'e', 'f']); $collection->every(4); // ['a', 'e'] ~~~ 您可以選擇將offset作為第二個參數傳遞: ~~~ $collection->every(4, 1); // ['b', 'f'] ~~~ #### [](https://octobercms.com/docs/services/collections#method-filter)`filter()` 該`filter`方法通過給定的回調過濾集合,僅保留那些通過給定的真實性測試的項目: ~~~ $collection = new Collection([1, 2, 3, 4]); $filtered = $collection->filter(function ($item) { return $item > 2; }); $filtered->all(); // [3, 4] ~~~ 有關的反函數`filter`,請參見[拒絕](https://octobercms.com/docs/services/collections#method-reject)方法。 #### [](https://octobercms.com/docs/services/collections#method-first)`first()` 該`first`方法返回通過給定的真實性測試的集合中的第一個元素: ~~~ new Collection([1, 2, 3, 4])->first(function ($value, $key) { return $value > 2; }); // 3 ~~~ 您也可以`first`不帶任何參數調用該方法以獲取集合中的第一個元素。如果集合為空,`null`則返回: ~~~ new Collection([1, 2, 3, 4])->first(); // 1 ~~~ #### [](https://octobercms.com/docs/services/collections#method-first-where)`firstWhere()` 該`firstWhere`方法返回具有給定鍵/值對的集合中的第一個元素: ~~~ $collection = collect([ ['name' => 'Regena', 'age' => null], ['name' => 'Linda', 'age' => 14], ['name' => 'Diego', 'age' => 23], ['name' => 'Linda', 'age' => 84], ]); $collection->firstWhere('name', 'Linda'); // ['name' => 'Linda', 'age' => 14] ~~~ 您也可以`firstWhere`使用運算符來調用該方法: ~~~ $collection->firstWhere('age', '>=', 18); // ['name' => 'Diego', 'age' => 23] ~~~ 像[where](https://octobercms.com/docs/services/collections#method-where)方法一樣,您可以將一個參數傳遞給該`firstWhere`方法。在這種情況下,該`firstWhere`方法將返回給定項鍵值為“ truthy”的第一項: ~~~ $collection->firstWhere('age'); // ['name' => 'Linda', 'age' => 14] ~~~ #### [](https://octobercms.com/docs/services/collections#method-flatmap)`flatMap()` 該`flatMap`方法遍歷集合并將每個值傳遞給給定的回調。回調程序可以自由修改并返回它,從而形成一個新的修改后的集合。然后,將數組展平一個級別: ~~~ $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']; ~~~ #### [](https://octobercms.com/docs/services/collections#method-flatten)`flatten()` 該`flatten`方法將多維集合展平為一個維: ~~~ $collection = new Collection(['name' => 'peter', 'languages' => ['php', 'javascript']]); $flattened = $collection->flatten(); $flattened->all(); // ['peter', 'php', 'javascript']; ~~~ #### [](https://octobercms.com/docs/services/collections#method-flip)`flip()` 該`flip`方法將集合的鍵交換為其相應的值: ~~~ $collection = new Collection(['name' => 'peter', 'platform' => 'october']); $flipped = $collection->flip(); $flipped->all(); // ['peter' => 'name', 'october' => 'platform'] ~~~ #### [](https://octobercms.com/docs/services/collections#method-forget)`forget()` 該`forget`方法通過鍵將其從集合中移除: ~~~ $collection = new Collection(['name' => 'peter', 'platform' => 'october']); $collection->forget('name'); $collection->all(); // ['platform' => 'october'] ~~~ > **注意:**與大多數其他收集方法不同,`forget`它不返回新的修改后的收集;它修改了它被調用的集合。 #### [](https://octobercms.com/docs/services/collections#method-forpage)`forPage()` 該`forPage`方法返回一個新集合,其中包含將在給定頁碼上顯示的項目: ~~~ $collection = new Collection([1, 2, 3, 4, 5, 6, 7, 8, 9])->forPage(2, 3); $collection->all(); // [4, 5, 6] ~~~ 該方法需要頁碼和每頁顯示的項目數。 #### [](https://octobercms.com/docs/services/collections#method-get)`get()` 該`get`方法以給定的鍵返回該項目。如果密鑰不存在,`null`則返回: ~~~ $collection = new Collection(['name' => 'peter', 'platform' => 'october']); $value = $collection->get('name'); // peter ~~~ 您可以選擇將默認值作為第二個參數傳遞: ~~~ $collection = new Collection(['name' => 'peter', 'platform' => 'october']); $value = $collection->get('foo', 'default-value'); // default-value ~~~ 您甚至可以將回調作為默認值傳遞。如果指定的鍵不存在,則將返回回調結果: ~~~ $collection->get('email', function () { return 'default-value'; }); // default-value ~~~ #### [](https://octobercms.com/docs/services/collections#method-groupby)`groupBy()` 該`groupBy`方法通過給定鍵將集合的項目分組: ~~~ $collection = new Collection([ ['account_id' => 'account-x10', 'product' => 'Bookcase'], ['account_id' => 'account-x10', 'product' => 'Chair'], ['account_id' => 'account-x11', 'product' => 'Desk'], ]); $grouped = $collection->groupBy('account_id'); $grouped->toArray(); /* [ 'account-x10' => [ ['account_id' => 'account-x10', 'product' => 'Bookcase'], ['account_id' => 'account-x10', 'product' => 'Chair'], ], 'account-x11' => [ ['account_id' => 'account-x11', 'product' => 'Desk'], ], ] */ ~~~ 除了傳遞字符串外`key`,您還可以傳遞回調。回調應返回您希望通過以下方式鍵入組的值: ~~~ $grouped = $collection->groupBy(function ($item, $key) { return substr($item['account_id'], -3); }); $grouped->toArray(); /* [ 'x10' => [ ['account_id' => 'account-x10', 'product' => 'Bookcase'], ['account_id' => 'account-x10', 'product' => 'Chair'], ], 'x11' => [ ['account_id' => 'account-x11', 'product' => 'Desk'], ], ] */ ~~~ #### [](https://octobercms.com/docs/services/collections#method-has)`has()` 該`has`方法確定集合中是否存在給定鍵: ~~~ $collection = new Collection(['account_id' => 1, 'product' => 'Desk']); $collection->has('email'); // false ~~~ #### [](https://octobercms.com/docs/services/collections#method-implode)`implode()` 該`implode`方法將項目合并到一個集合中。其參數取決于集合中項目的類型。 如果集合包含數組或對象,則應傳遞希望加入的屬性的鍵,以及希望在值之間放置的“ glue”字符串: ~~~ $collection = new Collection([ ['account_id' => 1, 'product' => 'Chair'], ['account_id' => 2, 'product' => 'Desk'], ]); $collection->implode('product', ', '); // Chair, Desk ~~~ 如果集合包含簡單的字符串或數字值,則只需將“ glue”作為唯一的參數傳遞給方法: ~~~ new Collection([1, 2, 3, 4, 5])->implode('-'); // '1-2-3-4-5' ~~~ #### [](https://octobercms.com/docs/services/collections#method-intersect)`intersect()` 該`intersect`方法刪除給定`array`或集合中不存在的所有值: ~~~ $collection = new Collection(['Desk', 'Sofa', 'Chair']); $intersect = $collection->intersect(['Desk', 'Chair', 'Bookcase']); $intersect->all(); // [0 => 'Desk', 2 => 'Chair'] ~~~ 如您所見,生成的集合將保留原始集合的鍵。 #### [](https://octobercms.com/docs/services/collections#method-intersectbykeys)`intersectByKeys()` 該`intersectByKeys`方法從原始集合中刪除給定`array`或集合中不存在的所有鍵: ~~~ $collection = collect([ 'serial' => 'UX301', 'type' => 'screen', 'year' => 2009 ]); $intersect = $collection->intersectByKeys([ 'reference' => 'UX404', 'type' => 'tab', 'year' => 2011 ]); $intersect->all(); // ['type' => 'screen', 'year' => 2009] ~~~ #### [](https://octobercms.com/docs/services/collections#method-isempty)`isEmpty()` 如果集合為空,則`isEmpty`返回該方法`true`;否則返回false。否則`false`返回: ~~~ new Collection([])->isEmpty(); // true ~~~ #### [](https://octobercms.com/docs/services/collections#method-isnotempty)`isNotEmpty()` 如果集合不為空,則`isNotEmpty`返回該方法`true`;否則返回false。否則,`false`返回: ~~~ collect([])->isNotEmpty(); // false ~~~ #### [](https://octobercms.com/docs/services/collections#method-join)`join()` 該`join`方法使用字符串將集合的值連接起來: ~~~ collect(['a', 'b', 'c'])->join(', '); // 'a, b, c' collect(['a', 'b', 'c'])->join(', ', ', and '); // 'a, b, and c' collect(['a', 'b'])->join(', ', ' and '); // 'a and b' collect(['a'])->join(', ', ' and '); // 'a' collect([])->join(', ', ' and '); // '' ~~~ #### [](https://octobercms.com/docs/services/collections#method-keyby)`keyBy()` 通過給定的鍵為集合設置鍵: ~~~ $collection = new Collection([ ['product_id' => 'prod-100', 'name' => 'chair'], ['product_id' => 'prod-200', 'name' => 'desk'], ]); $keyed = $collection->keyBy('product_id'); $keyed->all(); /* [ 'prod-100' => ['product_id' => 'prod-100', 'name' => 'Chair'], 'prod-200' => ['product_id' => 'prod-200', 'name' => 'Desk'], ] */ ~~~ 如果多個項目具有相同的鍵,則只有最后一個將出現在新集合中。 您還可以傳遞自己的回調,該回調應通過以下方式返回值以鍵值集合: ~~~ $keyed = $collection->keyBy(function ($item) { return strtoupper($item['product_id']); }); $keyed->all(); /* [ 'PROD-100' => ['product_id' => 'prod-100', 'name' => 'Chair'], 'PROD-200' => ['product_id' => 'prod-200', 'name' => 'Desk'], ] */ ~~~ #### [](https://octobercms.com/docs/services/collections#method-keys)`keys()` 該`keys`方法返回集合的所有鍵: ~~~ $collection = new Collection([ 'prod-100' => ['product_id' => 'prod-100', 'name' => 'Chair'], 'prod-200' => ['product_id' => 'prod-200', 'name' => 'Desk'], ]); $keys = $collection->keys(); $keys->all(); // ['prod-100', 'prod-200'] ~~~ #### [](https://octobercms.com/docs/services/collections#method-last)`last()` 該`last`方法返回通過給定的真實性測試的集合中的最后一個元素: ~~~ new Collection([1, 2, 3, 4])->last(function ($key, $value) { return $value < 3; }); // 2 ~~~ 您也可以`last`不帶任何參數的方法來獲取集合中的最后一個元素。如果集合為空,則`null`返回。 ~~~ new Collection([1, 2, 3, 4])->last(); // 4 ~~~ #### [](https://octobercms.com/docs/services/collections#method-map)`map()` 該`map`方法遍歷集合并將每個值傳遞給給定的回調。回調程序可以自由修改并返回它,從而形成一個新的修改后的集合: ~~~ $collection = new Collection([1, 2, 3, 4, 5]); $multiplied = $collection->map(function ($item, $key) { return $item * 2; }); $multiplied->all(); // [2, 4, 6, 8, 10] ~~~ > **注意:**與大多數其他收集方法一樣,`map`返回一個新的收集實例。它不會修改調用的集合。如果要轉換原始集合,請使用[`transform`](https://octobercms.com/docs/services/collections#method-transform)方法。 #### [](https://octobercms.com/docs/services/collections#method-mapinto)`mapInto()` 該`mapInto()`方法遍歷集合,通過將值傳遞給構造函數來創建給定類的新實例: ~~~ class Currency { /** * Create a new currency instance. * * @param string $code * @return void */ function __construct(string $code) { $this->code = $code; } } $collection = collect(['USD', 'EUR', 'GBP']); $currencies = $collection->mapInto(Currency::class); $currencies->all(); // [Currency('USD'), Currency('EUR'), Currency('GBP')] ~~~ #### [](https://octobercms.com/docs/services/collections#method-mapspread)`mapSpread()` 該`mapSpread`方法遍歷集合的項目,將每個嵌套項目的值傳遞到給定的回調中。回調程序可以自由修改并返回該項目,從而形成一個新的已修改項目集合: ~~~ $collection = collect([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); $chunks = $collection->chunk(2); $sequence = $chunks->mapSpread(function ($even, $odd) { return $even + $odd; }); $sequence->all(); // [1, 5, 9, 13, 17] ~~~ #### [](https://octobercms.com/docs/services/collections#method-maptogroups)`mapToGroups()` 該`mapToGroups`方法通過給定的回調將集合的項目分組。回調應返回一個包含單個鍵/值對的關聯數組,從而形成一個新的分組值集合: ~~~ $collection = collect([ [ 'name' => 'John Doe', 'department' => 'Sales', ], [ 'name' => 'Jane Doe', 'department' => 'Sales', ], [ 'name' => 'Johnny Doe', 'department' => 'Marketing', ] ]); $grouped = $collection->mapToGroups(function ($item, $key) { return [$item['department'] => $item['name']]; }); $grouped->toArray(); /* [ 'Sales' => ['John Doe', 'Jane Doe'], 'Marketing' => ['Johnny Doe'], ] */ $grouped->get('Sales')->all(); // ['John Doe', 'Jane Doe'] ~~~ #### [](https://octobercms.com/docs/services/collections#method-mapwithkeys)`mapWithKeys()` 該`mapWithKeys`方法遍歷集合并將每個值傳遞給給定的回調。回調應返回一個包含單個鍵/值對的關聯數組: ~~~ $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', ] */ ~~~ #### [](https://octobercms.com/docs/services/collections#method-max)`max()` 該`max`方法返回給定鍵的最大值: ~~~ $max = collect([['foo' => 10], ['foo' => 20]])->max('foo'); // 20 $max = collect([1, 2, 3, 4, 5])->max(); // 5 ~~~ #### [](https://octobercms.com/docs/services/collections#method-median)`median()` 該`median`方法返回給定鍵的[中間值](https://en.wikipedia.org/wiki/Median): ~~~ $median = collect([['foo' => 10], ['foo' => 10], ['foo' => 20], ['foo' => 40]])->median('foo'); // 15 $median = collect([1, 1, 2, 4])->median(); // 1.5 ~~~ #### [](https://octobercms.com/docs/services/collections#method-merge)`merge()` 該`merge`方法將給定的數組或集合與原始集合合并。如果給定項目中的字符串鍵與原始集合中的字符串鍵匹配,則給定項目的值將覆蓋原始集合中的值: ~~~ $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'] ~~~ #### [](https://octobercms.com/docs/services/collections#method-mergerecursive)`mergeRecursive()` 該`mergeRecursive`方法將給定的數組或集合與原始集合遞歸合并。如果給定項中的字符串鍵與原始集合中的字符串鍵匹配,則這些鍵的值將合并到一個數組中,然后遞歸完成: ~~~ $collection = collect(['product_id' => 1, 'price' => 100]); $merged = $collection->mergeRecursive(['product_id' => 2, 'price' => 200, 'discount' => false]); $merged->all(); // ['product_id' => [1, 2], 'price' => [100, 200], 'discount' => false] ~~~ #### [](https://octobercms.com/docs/services/collections#method-min)`min()` 該`min`方法返回給定鍵的最小值: ~~~ $min = collect([['foo' => 10], ['foo' => 20]])->min('foo'); // 10 $min = collect([1, 2, 3, 4, 5])->min(); // 1 ~~~ #### [](https://octobercms.com/docs/services/collections#method-mode)`mode()` 該`mode`方法返回給定鍵的[模式值](https://en.wikipedia.org/wiki/Mode_(statistics)): ~~~ $mode = collect([['foo' => 10], ['foo' => 10], ['foo' => 20], ['foo' => 40]])->mode('foo'); // [10] $mode = collect([1, 1, 2, 4])->mode(); // [1] ~~~ #### [](https://octobercms.com/docs/services/collections#method-nth)`nth()` 該`nth`方法創建一個由第n個元素組成的新集合: ~~~ $collection = collect(['a', 'b', 'c', 'd', 'e', 'f']); $collection->nth(4); // ['a', 'e'] ~~~ 您可以選擇將偏移量作為第二個參數傳遞: ~~~ $collection->nth(4, 1); // ['b', 'f'] ~~~ #### [](https://octobercms.com/docs/services/collections#method-only)`only()` 該`only`方法使用指定的鍵返回集合中的項目: ~~~ $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](https://octobercms.com/docs/services/collections#method-except)方法。 #### [](https://octobercms.com/docs/services/collections#method-pad)`pad()` 該`pad`方法將使用給定值填充數組,直到數組達到指定大小為止。此方法的行為類似于[array\_pad](https://secure.php.net/manual/en/function.array-pad.php)PHP函數。 要向左填充,應指定一個負數。如果給定大小的絕對值小于或等于數組的長度,則不會進行填充: ~~~ $collection = collect(['A', 'B', 'C']); $filtered = $collection->pad(5, 0); $filtered->all(); // ['A', 'B', 'C', 0, 0] $filtered = $collection->pad(-5, 0); $filtered->all(); // [0, 0, 'A', 'B', 'C'] ~~~ #### [](https://octobercms.com/docs/services/collections#method-partition)`partition()` 該`partition`方法可以與`list`PHP函數結合使用,以將通過給定真值測試的元素與未通過的元素分開: ~~~ $collection = collect([1, 2, 3, 4, 5, 6]); list($underThree, $equalOrAboveThree) = $collection->partition(function ($i) { return $i < 3; }); $underThree->all(); // [1, 2] $equalOrAboveThree->all(); // [3, 4, 5, 6] ~~~ #### [](https://octobercms.com/docs/services/collections#method-pipe)`pipe()` 該`pipe`方法將集合傳遞給給定的回調并返回結果: ~~~ $collection = collect([1, 2, 3]); $piped = $collection->pipe(function ($collection) { return $collection->sum(); }); // 6 ~~~ #### [](https://octobercms.com/docs/services/collections#method-pluck)`pluck()` 該`pluck`方法檢索給定鍵的所有集合值: ~~~ $collection = new Collection([ ['product_id' => 'prod-100', 'name' => 'Chair'], ['product_id' => 'prod-200', 'name' => 'Desk'], ]); $plucked = $collection->pluck('name'); $plucked->all(); // ['Chair', 'Desk'] ~~~ 您還可以指定希望如何對結果集合進行鍵控: ~~~ $plucked = $collection->pluck('name', 'product_id'); $plucked->all(); // ['prod-100' => 'Desk', 'prod-200' => 'Chair'] ~~~ #### [](https://octobercms.com/docs/services/collections#method-pop)`pop()` 該`pop`方法從集合中刪除并返回最后一個項目: ~~~ $collection = new Collection([1, 2, 3, 4, 5]); $collection->pop(); // 5 $collection->all(); // [1, 2, 3, 4] ~~~ #### [](https://octobercms.com/docs/services/collections#method-prepend)`prepend()` 該`prepend`方法將一個項目添加到集合的開頭: ~~~ $collection = new Collection([1, 2, 3, 4, 5]); $collection->prepend(0); $collection->all(); // [0, 1, 2, 3, 4, 5] ~~~ #### [](https://octobercms.com/docs/services/collections#method-pull)`pull()` 該`pull`方法通過其鍵從集合中刪除并返回一個項目: ~~~ $collection = new Collection(['product_id' => 'prod-100', 'name' => 'Desk']); $collection->pull('name'); // 'Desk' $collection->all(); // ['product_id' => 'prod-100'] ~~~ #### [](https://octobercms.com/docs/services/collections#method-push)`push()` 該`push`方法將一個項目附加到集合的末尾: ~~~ $collection = new Collection([1, 2, 3, 4]); $collection->push(5); $collection->all(); // [1, 2, 3, 4, 5] ~~~ #### [](https://octobercms.com/docs/services/collections#method-put)`put()` 該`put`方法在集合中設置給定的鍵和值: ~~~ $collection = new Collection(['product_id' => 1, 'name' => 'Desk']); $collection->put('price', 100); $collection->all(); // ['product_id' => 1, 'name' => 'Desk', 'price' => 100] ~~~ #### [](https://octobercms.com/docs/services/collections#method-random)`random()` 該`random`方法從集合中返回一個隨機項目: ~~~ $collection = new Collection([1, 2, 3, 4, 5]); $collection->random(); // 4 - (retrieved randomly) ~~~ 您可以選擇將整數傳遞給`random`。如果該整數大于`1`,則返回項目的集合: ~~~ $random = $collection->random(3); $random->all(); // [2, 4, 5] - (retrieved randomly) ~~~ #### [](https://octobercms.com/docs/services/collections#method-reduce)`reduce()` 該`reduce`方法將集合簡化為單個值,將每次迭代的結果傳遞到后續迭代中: ~~~ $collection = new Collection([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 ~~~ #### [](https://octobercms.com/docs/services/collections#method-reject)`reject()` 該`reject`方法使用給定的回調過濾集合。回調應返回`true`希望從結果集合中刪除的所有項目: ~~~ $collection = new Collection([1, 2, 3, 4]); $filtered = $collection->reject(function ($item) { return $item > 2; }); $filtered->all(); // [1, 2] ~~~ 有關`reject`方法的逆過程,請參見[`filter`](https://octobercms.com/docs/services/collections#method-filter)方法。 #### [](https://octobercms.com/docs/services/collections#method-replace)`replace()` 該`replace`方法的行為類似于`merge`;但是,除了使用字符串鍵覆蓋匹配項之外,該`replace`方法還將覆蓋集合中具有匹配數字鍵的項: ~~~ $collection = collect(['Taylor', 'Abigail', 'James']); $replaced = $collection->replace([1 => 'Victoria', 3 => 'Finn']); $replaced->all(); // ['Taylor', 'Victoria', 'James', 'Finn'] ~~~ #### [](https://octobercms.com/docs/services/collections#method-replacerecursive)`replaceRecursive()` 此方法的工作方式與相似`replace`,但它將重現為數組并將相同的替換過程應用于內部值: ~~~ $collection = collect(['Taylor', 'Abigail', ['James', 'Victoria', 'Finn']]); $replaced = $collection->replaceRecursive(['Charlie', 2 => [1 => 'King']]); $replaced->all(); // ['Charlie', 'Abigail', ['James', 'King', 'Finn']] ~~~ #### [](https://octobercms.com/docs/services/collections#method-reverse)`reverse()` 該`reverse`方法反轉集合項的順序: ~~~ $collection = new Collection([1, 2, 3, 4, 5]); $reversed = $collection->reverse(); $reversed->all(); // [5, 4, 3, 2, 1] ~~~ #### [](https://octobercms.com/docs/services/collections#method-search)`search()` 該`search`方法在集合中搜索給定的值,如果找到,則返回其鍵。如果找不到該項目,`false`則返回。 ~~~ $collection = new Collection([2, 4, 6, 8]); $collection->search(4); // 1 ~~~ 使用“松散”比較完成搜索。要使用嚴格比較,`true`請將第二個參數傳遞給該方法: ~~~ $collection->search('4', true); // false ~~~ 或者,您可以傳遞自己的回調來搜索通過真實性測試的第一項: ~~~ $collection->search(function ($item, $key) { return $item > 5; }); // 2 ~~~ #### [](https://octobercms.com/docs/services/collections#method-shift)`shift()` 該`shift`方法從集合中刪除并返回第一項: ~~~ $collection = new Collection([1, 2, 3, 4, 5]); $collection->shift(); // 1 $collection->all(); // [2, 3, 4, 5] ~~~ #### [](https://octobercms.com/docs/services/collections#method-shuffle)`shuffle()` 該`shuffle`方法隨機地隨機移動集合中的項目: ~~~ $collection = new Collection([1, 2, 3, 4, 5]); $shuffled = $collection->shuffle(); $shuffled->all(); // [3, 2, 5, 1, 4] (generated randomly) ~~~ #### [](https://octobercms.com/docs/services/collections#method-skip)`skip()` 該`skip`方法返回一個新的集合,沒有第一個給定的項目數量: ~~~ $collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); $collection = $collection->skip(4); $collection->all(); // [5, 6, 7, 8, 9, 10] ~~~ #### [](https://octobercms.com/docs/services/collections#method-slice)`slice()` 該`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`](https://octobercms.com/docs/services/collections#method-values)方法為它們重新編制索引。 #### [](https://octobercms.com/docs/services/collections#method-some)`some()` 該[`contains`](https://octobercms.com/docs/services/collections#method-contains)方法的別名。 #### [](https://octobercms.com/docs/services/collections#method-sort)`sort()` 該`sort`方法對集合進行排序: ~~~ $collection = new Collection([5, 3, 1, 2, 4]); $sorted = $collection->sort(); $sorted->values()->all(); // [1, 2, 3, 4, 5] ~~~ 排序的集合保留原始數組鍵。在此示例中,我們使用該[`values`](https://octobercms.com/docs/services/collections#method-values)方法將密鑰重置為連續編號的索引。 有關對嵌套數組或對象的集合進行排序的信息,請參見[`sortBy`](https://octobercms.com/docs/services/collections#method-sortby)和[`sortByDesc`](https://octobercms.com/docs/services/collections#method-sortbydesc)方法。 如果您的排序需求更高級,則可以`sort`使用自己的算法將回調傳遞給。請參閱上的PHP文檔[`usort`](http://php.net/manual/en/function.usort.php#refsect1-function.usort-parameters),這是集合的`sort`方法在后臺調用的內容。 #### [](https://octobercms.com/docs/services/collections#method-sortby)`sortBy()` 該`sortBy`方法按給定鍵對集合進行排序: ~~~ $collection = new Collection([ ['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], ] */ ~~~ 排序的集合保留原始數組鍵。在此示例中,我們使用該[`values`](https://octobercms.com/docs/services/collections#method-values)方法將密鑰重置為連續編號的索引。 您還可以傳遞自己的回調以確定如何對集合值進行排序: ~~~ $collection = new Collection([ ['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']], ] */ ~~~ #### [](https://octobercms.com/docs/services/collections#method-sortbydesc)`sortByDesc()` 此方法與該方法具有相同的簽名[`sortBy`](https://octobercms.com/docs/services/collections#method-sortby),但是將以相反的順序對集合進行排序。 #### [](https://octobercms.com/docs/services/collections#method-sortkeys)`sortKeys()` 該`sortKeys`方法通過基礎關聯數組的鍵對集合進行排序: ~~~ $collection = collect([ 'id' => 22345, 'first' => 'John', 'last' => 'Doe', ]); $sorted = $collection->sortKeys(); $sorted->all(); /* [ 'first' => 'John', 'id' => 22345, 'last' => 'Doe', ] */ ~~~ #### [](https://octobercms.com/docs/services/collections#method-sortkeysdesc)`sortKeysDesc()` 此方法與該方法具有相同的簽名[`sortKeys`](https://octobercms.com/docs/services/collections#method-sortkeys),但是將以相反的順序對集合進行排序。 #### [](https://octobercms.com/docs/services/collections#method-splice)`splice()` 該`splice`方法刪除并返回從指定索引處開始的項目切片: ~~~ $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] ~~~ #### [](https://octobercms.com/docs/services/collections#method-splice)`splice()` 該`splice`方法刪除并返回從指定索引處開始的項目切片: ~~~ $collection = new Collection([1, 2, 3, 4, 5]); $chunk = $collection->splice(2); $chunk->all(); // [3, 4, 5] $collection->all(); // [1, 2] ~~~ 您可以傳遞第二個參數來限制結果塊的大小: ~~~ $collection = new Collection([1, 2, 3, 4, 5]); $chunk = $collection->splice(2, 1); $chunk->all(); // [3] $collection->all(); // [1, 2, 4, 5] ~~~ 此外,您可以傳遞包含新項目的第三個參數來替換從集合中刪除的項目: ~~~ $collection = new Collection([1, 2, 3, 4, 5]); $chunk = $collection->splice(2, 1, [10, 11]); $chunk->all(); // [3] $collection->all(); // [1, 2, 10, 11, 4, 5] ~~~ #### [](https://octobercms.com/docs/services/collections#method-split)`split()` 該`split`方法將集合分為給定數量的組: ~~~ $collection = collect([1, 2, 3, 4, 5]); $groups = $collection->split(3); $groups->toArray(); // [[1, 2], [3, 4], [5]] ~~~ #### [](https://octobercms.com/docs/services/collections#method-sum)`sum()` 該`sum`方法返回集合中所有項目的總和: ~~~ new Collection([1, 2, 3, 4, 5])->sum(); // 15 ~~~ 如果集合包含嵌套的數組或對象,則應傳遞一個鍵來確定要求和的值: ~~~ $collection = new Collection([ ['name' => 'JavaScript: The Good Parts', 'pages' => 176], ['name' => 'JavaScript: The Definitive Guide', 'pages' => 1096], ]); $collection->sum('pages'); // 1272 ~~~ 另外,您可以傳遞自己的回調以確定匯總哪些集合值: ~~~ $collection = new Collection([ ['name' => 'Chair', 'colors' => ['Black']], ['name' => 'Desk', 'colors' => ['Black', 'Mahogany']], ['name' => 'Bookcase', 'colors' => ['Red', 'Beige', 'Brown']], ]); $collection->sum(function ($product) { return count($product['colors']); }); // 6 ~~~ #### [](https://octobercms.com/docs/services/collections#method-take)`take()` 該`take`方法返回具有指定數量的項目的新集合: ~~~ $collection = new Collection([0, 1, 2, 3, 4, 5]); $chunk = $collection->take(3); $chunk->all(); // [0, 1, 2] ~~~ 您還可以傳遞一個負整數以從集合的末尾獲取指定數量的項目: ~~~ $collection = new Collection([0, 1, 2, 3, 4, 5]); $chunk = $collection->take(-2); $chunk->all(); // [4, 5] ~~~ #### [](https://octobercms.com/docs/services/collections#method-tap)`tap()` 該`tap`方法將集合傳遞給給定的回調,使您可以在特定位置“點擊”集合,并在不影響集合本身的情況下對項目進行一些操作: ~~~ collect([2, 4, 3, 1, 5]) ->sort() ->tap(function ($collection) { Log::debug('Values after sorting', $collection->values()->toArray()); }) ->shift(); // 1 ~~~ #### [](https://octobercms.com/docs/services/collections#method-times)`times()` 靜態`times`方法通過調用給定次數的回調來創建新集合: ~~~ $collection = Collection::times(10, function ($number) { return $number * 9; }); $collection->all(); // [9, 18, 27, 36, 45, 54, 63, 72, 81, 90] ~~~ 與工廠結合以創建[Eloquent](https://octobercms.com/docs/%7B%7Bversion%7D%7D/eloquent)模型時,此方法很有用: ~~~ $categories = Collection::times(3, function ($number) { return factory(Category::class)->create(['name' => "Category No. $number"]); }); $categories->all(); /* [ ['id' => 1, 'name' => 'Category No. 1'], ['id' => 2, 'name' => 'Category No. 2'], ['id' => 3, 'name' => 'Category No. 3'], ] */ ~~~ #### [](https://octobercms.com/docs/services/collections#method-toarray)`toArray()` 該`toArray`方法將集合轉換為純PHP`array`。如果集合的值是[數據庫模型](https://octobercms.com/docs/database/model),則這些模型也將轉換為數組: ~~~ $collection = new Collection(['name' => 'Desk', 'price' => 200]); $collection->toArray(); /* [ ['name' => 'Desk', 'price' => 200], ] */ ~~~ > **注意:**`toArray`還將所有嵌套對象轉換為數組。如果要按原樣獲取基礎數組,請使用[`all`](https://octobercms.com/docs/services/collections#method-all)方法。 #### [](https://octobercms.com/docs/services/collections#method-tojson)`toJson()` 該`toJson`方法將集合轉換為JSON: ~~~ $collection = new Collection(['name' => 'Desk', 'price' => 200]); $collection->toJson(); // '{"name":"Desk","price":200}' ~~~ #### [](https://octobercms.com/docs/services/collections#method-transform)`transform()` 該`transform`方法遍歷集合,并使用集合中的每個項目調用給定的回調。集合中的項目將由回調返回的值替換: ~~~ $collection = new Collection([1, 2, 3, 4, 5]); $collection->transform(function ($item, $key) { return $item * 2; }); $collection->all(); // [2, 4, 6, 8, 10] ~~~ > **注意:**與大多數其他收集方法不同,`transform`修改收集本身。如果您想創建一個新的集合,請使用[`map`](https://octobercms.com/docs/services/collections#method-map)方法。 #### [](https://octobercms.com/docs/services/collections#method-union)`union()` 該`union`方法將給定數組添加到集合中。如果給定數組包含原始集合中已經存在的鍵,則首選原始集合的值: ~~~ $collection = collect([1 => ['a'], 2 => ['b']]); $union = $collection->union([3 => ['c'], 1 => ['b']]); $union->all(); // [1 => ['a'], 2 => ['b'], 3 => ['c']] ~~~ #### [](https://octobercms.com/docs/services/collections#method-unique)`unique()` 該`unique`方法返回集合中的所有唯一項。返回的集合保留原始數組鍵,因此在此示例中,我們將使用[`values`](https://octobercms.com/docs/services/collections#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'], ] */ ~~~ 該`unique`方法在檢查項目值時使用“松散”比較,這意味著具有整數值的字符串將被視為等于相同值的整數。使用該[`uniqueStrict`](https://octobercms.com/docs/services/collections#method-uniquestrict)方法使用“嚴格”比較進行過濾。 #### [](https://octobercms.com/docs/services/collections#method-uniquestrict)`uniqueStrict()` 此方法與該方法具有相同的簽名[`unique`](https://octobercms.com/docs/services/collections#method-unique);但是,使用“嚴格”比較來比較所有值。 #### [](https://octobercms.com/docs/services/collections#method-unless)`unless()` 該`unless`方法將執行給定的回調,除非給該方法的第一個參數求值為`true`: ~~~ $collection = collect([1, 2, 3]); $collection->unless(true, function ($collection) { return $collection->push(4); }); $collection->unless(false, function ($collection) { return $collection->push(5); }); $collection->all(); // [1, 2, 3, 5] ~~~ 有關的逆`unless`,請參見[`when`](https://octobercms.com/docs/services/collections#method-when)方法。 #### [](https://octobercms.com/docs/services/collections#method-unlessempty)`unlessEmpty()` 該[`whenNotEmpty`](https://octobercms.com/docs/services/collections#method-whennotempty)方法的別名。 #### [](https://octobercms.com/docs/services/collections#method-unlessnotempty)`unlessNotEmpty()` 該[`whenEmpty`](https://octobercms.com/docs/services/collections#method-whenempty)方法的別名。 #### [](https://octobercms.com/docs/services/collections#method-unwrap)`unwrap()` 靜態`unwrap`方法在適用時從給定值返回集合的基礎項: ~~~ Collection::unwrap(collect('John Doe')); // ['John Doe'] Collection::unwrap(['John Doe']); // ['John Doe'] Collection::unwrap('John Doe'); // 'John Doe' ~~~ #### [](https://octobercms.com/docs/services/collections#method-values)`values()` 該`values`方法返回一個新集合,其鍵重置為連續的整數: ~~~ $collection = new Collection([ 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], ] */ ~~~ #### [](https://octobercms.com/docs/services/collections#method-when)`when()` `when`當給該方法的第一個參數計算為時,該方法將執行給定的回調`true`: ~~~ $collection = collect([1, 2, 3]); $collection->when(true, function ($collection) { return $collection->push(4); }); $collection->when(false, function ($collection) { return $collection->push(5); }); $collection->all(); // [1, 2, 3, 4] ~~~ 有關的逆`when`,請參見[`unless`](https://octobercms.com/docs/services/collections#method-unless)方法。 #### [](https://octobercms.com/docs/services/collections#method-whenempty)`whenEmpty()` `whenEmpty`當集合為空時,該方法將執行給定的回調: ~~~ $collection = collect(['michael', 'tom']); $collection->whenEmpty(function ($collection) { return $collection->push('adam'); }); $collection->all(); // ['michael', 'tom'] $collection = collect(); $collection->whenEmpty(function ($collection) { return $collection->push('adam'); }); $collection->all(); // ['adam'] $collection = collect(['michael', 'tom']); $collection->whenEmpty(function ($collection) { return $collection->push('adam'); }, function ($collection) { return $collection->push('taylor'); }); $collection->all(); // ['michael', 'tom', 'taylor'] ~~~ 有關的逆`whenEmpty`,請參見[`whenNotEmpty`](https://octobercms.com/docs/services/collections#method-whennotempty)方法。 #### [](https://octobercms.com/docs/services/collections#method-whennotempty)`whenNotEmpty()` `whenNotEmpty`當集合不為空時,該方法將執行給定的回調: ~~~ $collection = collect(['michael', 'tom']); $collection->whenNotEmpty(function ($collection) { return $collection->push('adam'); }); $collection->all(); // ['michael', 'tom', 'adam'] $collection = collect(); $collection->whenNotEmpty(function ($collection) { return $collection->push('adam'); }); $collection->all(); // [] $collection = collect(); $collection->whenNotEmpty(function ($collection) { return $collection->push('adam'); }, function ($collection) { return $collection->push('taylor'); }); $collection->all(); // ['taylor'] ~~~ 有關的逆`whenNotEmpty`,請參見[`whenEmpty`](https://octobercms.com/docs/services/collections#method-whenempty)方法。 #### [](https://octobercms.com/docs/services/collections#method-where)`where()` 該`where`方法通過給定的鍵/值對過濾集合: ~~~ $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], ] */ ~~~ 該`where`方法在檢查項目值時使用“松散”比較,這意味著具有整數值的字符串將被視為等于相同值的整數。使用該[`whereStrict`](https://octobercms.com/docs/services/collections#method-wherestrict)方法使用“嚴格”比較進行過濾。 (可選)您可以將比較運算符作為第二個參數傳遞。 ~~~ $collection = collect([ ['name' => 'Jim', 'deleted_at' => '2019-01-01 00:00:00'], ['name' => 'Sally', 'deleted_at' => '2019-01-02 00:00:00'], ['name' => 'Sue', 'deleted_at' => null], ]); $filtered = $collection->where('deleted_at', '!=', null); $filtered->all(); /* [ ['name' => 'Jim', 'deleted_at' => '2019-01-01 00:00:00'], ['name' => 'Sally', 'deleted_at' => '2019-01-02 00:00:00'], ] */ ~~~ #### [](https://octobercms.com/docs/services/collections#method-wherestrict)`whereStrict()` 此方法與該方法具有相同的簽名[`where`](https://octobercms.com/docs/services/collections#method-where);但是,使用“嚴格”比較來比較所有值。 #### [](https://octobercms.com/docs/services/collections#method-wherebetween)`whereBetween()` 該`whereBetween`方法在給定范圍內過濾集合: ~~~ $collection = collect([ ['product' => 'Desk', 'price' => 200], ['product' => 'Chair', 'price' => 80], ['product' => 'Bookcase', 'price' => 150], ['product' => 'Pencil', 'price' => 30], ['product' => 'Door', 'price' => 100], ]); $filtered = $collection->whereBetween('price', [100, 200]); $filtered->all(); /* [ ['product' => 'Desk', 'price' => 200], ['product' => 'Bookcase', 'price' => 150], ['product' => 'Door', 'price' => 100], ] */ ~~~ #### [](https://octobercms.com/docs/services/collections#method-wherein)`whereIn()` 該`whereIn`方法通過給定數組中包含的給定鍵/值來過濾集合: ~~~ $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' => 'Desk', 'price' => 200], ['product' => 'Bookcase', 'price' => 150], ] */ ~~~ 該`whereIn`方法在檢查項目值時使用“松散”比較,這意味著具有整數值的字符串將被視為等于相同值的整數。使用該[`whereInStrict`](https://octobercms.com/docs/services/collections#method-whereinstrict)方法使用“嚴格”比較進行過濾。 #### [](https://octobercms.com/docs/services/collections#method-whereinstrict)`whereInStrict()` 此方法與該方法具有相同的簽名[`whereIn`](https://octobercms.com/docs/services/collections#method-wherein);但是,使用“嚴格”比較來比較所有值。 #### [](https://octobercms.com/docs/services/collections#method-whereinstanceof)`whereInstanceOf()` 該`whereInstanceOf`方法按給定的類類型過濾集合: ~~~ use App\User; use App\Post; $collection = collect([ new User, new User, new Post, ]); $filtered = $collection->whereInstanceOf(User::class); $filtered->all(); // [App\User, App\User] ~~~ #### [](https://octobercms.com/docs/services/collections#method-wherenotbetween)`whereNotBetween()` 該`whereNotBetween`方法在給定范圍內過濾集合: ~~~ $collection = collect([ ['product' => 'Desk', 'price' => 200], ['product' => 'Chair', 'price' => 80], ['product' => 'Bookcase', 'price' => 150], ['product' => 'Pencil', 'price' => 30], ['product' => 'Door', 'price' => 100], ]); $filtered = $collection->whereNotBetween('price', [100, 200]); $filtered->all(); /* [ ['product' => 'Chair', 'price' => 80], ['product' => 'Pencil', 'price' => 30], ] */ ~~~ #### [](https://octobercms.com/docs/services/collections#method-wherenotin)`whereNotIn()` 該`whereNotIn`方法通過給定鍵/值(未包含在給定數組中)過濾集合: ~~~ $collection = collect([ ['product' => 'Desk', 'price' => 200], ['product' => 'Chair', 'price' => 100], ['product' => 'Bookcase', 'price' => 150], ['product' => 'Door', 'price' => 100], ]); $filtered = $collection->whereNotIn('price', [150, 200]); $filtered->all(); /* [ ['product' => 'Chair', 'price' => 100], ['product' => 'Door', 'price' => 100], ] */ ~~~ 該`whereNotIn`方法在檢查項目值時使用“松散”比較,這意味著具有整數值的字符串將被視為等于相同值的整數。使用該[`whereNotInStrict`](https://octobercms.com/docs/services/collections#method-wherenotinstrict)方法使用“嚴格”比較進行過濾。 #### [](https://octobercms.com/docs/services/collections#method-wherenotinstrict)`whereNotInStrict()` 此方法與該方法具有相同的簽名[`whereNotIn`](https://octobercms.com/docs/services/collections#method-wherenotin);但是,使用“嚴格”比較來比較所有值。 #### [](https://octobercms.com/docs/services/collections#method-wherenotnull)`whereNotNull()` 該`whereNotNull`方法過濾給定鍵不為null的項目: ~~~ $collection = collect([ ['name' => 'Desk'], ['name' => null], ['name' => 'Bookcase'], ]); $filtered = $collection->whereNotNull('name'); $filtered->all(); /* [ ['name' => 'Desk'], ['name' => 'Bookcase'], ] */ ~~~ #### [](https://octobercms.com/docs/services/collections#method-wherenull)`whereNull()` 該`whereNull`方法過濾給定鍵為null的項目: ~~~ $collection = collect([ ['name' => 'Desk'], ['name' => null], ['name' => 'Bookcase'], ]); $filtered = $collection->whereNull('name'); $filtered->all(); /* [ ['name' => null], ] */ ~~~ #### [](https://octobercms.com/docs/services/collections#method-wrap)`wrap()` `wrap`如果適用,靜態方法會將給定值包裝在集合中: ~~~ $collection = Collection::wrap('John Doe'); $collection->all(); // ['John Doe'] $collection = Collection::wrap(['John Doe']); $collection->all(); // ['John Doe'] $collection = Collection::wrap(collect('John Doe')); $collection->all(); // ['John Doe'] ~~~ #### [](https://octobercms.com/docs/services/collections#method-zip)`zip()` 該`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>

                              哎呀哎呀视频在线观看