Skip to content

Commit ee4c34c

Browse files
committed
Add collections
1 parent 1ce169d commit ee4c34c

File tree

2 files changed

+286
-0
lines changed

2 files changed

+286
-0
lines changed

index.html

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1829,6 +1829,149 @@ <h6>Miscellaneous</h6>
18291829
// Returns the value it is given
18301830
$value = with(new Foo)->work();
18311831
</pre>
1832+
</section>
1833+
<section class="cmd-description grid-item">
1834+
<h4><a name="collection" href="#collection">Collection</a> <a href="https://laravel.com/docs/5.1/collections" title="Collections CLI @ Laravel Docs"><i class="icon-file-text"></i></a></h4>
1835+
<pre class="prettyprint lang-php">
1836+
// Creating Collections
1837+
collect([1, 2, 3]);
1838+
// Simply returns the underlying array represented by the collection:
1839+
$collection->all();
1840+
// Returns the average of all items in the collection:
1841+
$collection->avg();
1842+
// Breaks the collection into multiple, smaller collections of a given size:
1843+
$collection->chunk(4);
1844+
// Collapses a collection of arrays into a flat collection:
1845+
$collection->collapse();
1846+
// Determines whether the collection contains a given item:
1847+
$collection->contains('New York');
1848+
// Returns the total number of items in the collection:
1849+
$collection->count();
1850+
// Iterates over the items in the collection and passes each item to a given callback:
1851+
$collection = $collection->each(function ($item, $key) {
1852+
});
1853+
// Creates a new collection consisting of every n-th element:
1854+
$collection->every(4);
1855+
// Pass offset as the second argument:
1856+
$collection->every(4, 1);
1857+
// Returns all items in the collection except for those with the specified keys:
1858+
$collection->except(['price', 'discount']);
1859+
// Filters the collection by a given callback:
1860+
$filtered = $collection->filter(function ($item) {
1861+
return $item > 2;
1862+
});
1863+
// Returns the first element in the collection that passes a given truth test:
1864+
collect([1, 2, 3, 4])->first(function ($key, $value) {
1865+
return $value > 2;
1866+
});
1867+
// Flattens a multi-dimensional collection into a single dimension:
1868+
$flattened = $collection->flatten();
1869+
// Swaps the collection's keys with their corresponding values:
1870+
$flipped = $collection->flip();
1871+
// Removes an item from the collection by its key:
1872+
$collection->forget('name');
1873+
// Returns a new collection containing the items:
1874+
$chunk = $collection->forPage(2, 3);
1875+
// Returns the item at a given key. If the key does not exist, null is returned:
1876+
$value = $collection->get('name');
1877+
// Groups the collection's items by a given key:
1878+
$grouped = $collection->groupBy('account_id');
1879+
// Determines if a given key exists in the collection:
1880+
$collection->has('email');
1881+
// Joins the items in a collection:
1882+
$collection->implode('product', ', ');
1883+
// Removes any values that are not present in the given array or collection:
1884+
$intersect = $collection->intersect(['Desk', 'Chair', 'Bookcase']);
1885+
// Returns true if the collection is empty:
1886+
collect([])->isEmpty();
1887+
// Keys the collection by the given key:
1888+
$keyed = $collection->keyBy('product_id');
1889+
// Pass a callback, which should return the value to key the collection by:
1890+
$keyed = $collection->keyBy(function ($item) {
1891+
return strtoupper($item['product_id']);
1892+
});
1893+
// Returns all of the collection's keys:
1894+
$keys = $collection->keys();
1895+
// Returns the last element in the collection:
1896+
$collection->last();
1897+
// Iterates through the collection and passes each value to the given callback:
1898+
$multiplied = $collection->map(function ($item, $key) {
1899+
return $item * 2;
1900+
});
1901+
// Return the maximum value of a given key:
1902+
$max = collect([['foo' => 10], ['foo' => 20]])->max('foo');
1903+
$max = collect([1, 2, 3, 4, 5])->max();
1904+
// Merges the given array into the collection:
1905+
$merged = $collection->merge(['price' => 100, 'discount' => false]);
1906+
// Return the minimum value of a given key:
1907+
$min = collect([['foo' => 10], ['foo' => 20]])->min('foo');
1908+
$min = collect([1, 2, 3, 4, 5])->min();
1909+
// Returns the items in the collection with the specified keys:
1910+
$filtered = $collection->only(['product_id', 'name']);
1911+
// Retrieves all of the collection values for a given key:
1912+
$plucked = $collection->pluck('name');
1913+
// Removes and returns the last item from the collection:
1914+
$collection->pop();
1915+
// Adds an item to the beginning of the collection:
1916+
$collection->prepend(0);
1917+
// Pass a second argument to set the key of the prepended item:
1918+
$collection->prepend(0, 'zero');
1919+
// Removes and returns an item from the collection by its key:
1920+
$collection->pull('name');
1921+
// Appends an item to the end of the collection:
1922+
$collection->push(5);
1923+
// Sets the given key and value in the collection:
1924+
$collection->put('price', 100);
1925+
// Returns a random item from the collection:
1926+
$collection->random();
1927+
// Pass an integer to random. If that integer is more than 1, a collection of items is returned:
1928+
$random = $collection->random(3);
1929+
// Reduces the collection to a single value:
1930+
$total = $collection->reduce(function ($carry, $item) {
1931+
return $carry + $item;
1932+
});
1933+
// Filters the collection using the given callback:
1934+
$filtered = $collection->reject(function ($item) {
1935+
return $item > 2;
1936+
});
1937+
// Reverses the order of the collection's items:
1938+
$reversed = $collection->reverse();
1939+
// Searches the collection for the given value and returns its key if found:
1940+
$collection->search(4);
1941+
// Removes and returns the first item from the collection:
1942+
$collection->shift();
1943+
// Randomly shuffles the items in the collection:
1944+
$shuffled = $collection->shuffle();
1945+
// Returns a slice of the collection starting at the given index:
1946+
$slice = $collection->slice(4);
1947+
// Sorts the collection:
1948+
$sorted = $collection->sort();
1949+
// Sorts the collection by the given key:
1950+
$sorted = $collection->sortBy('price');
1951+
// Removes and returns a slice of items starting at the specified index:
1952+
$chunk = $collection->splice(2);
1953+
// Returns the sum of all items in the collection:
1954+
collect([1, 2, 3, 4, 5])->sum();
1955+
// Returns a new collection with the specified number of items:
1956+
$chunk = $collection->take(3);
1957+
// Converts the collection into a plain PHP array:
1958+
$collection->toArray();
1959+
// Converts the collection into JSON:
1960+
$collection->toJson();
1961+
// Iterates over the collection:
1962+
$collection->transform(function ($item, $key) {
1963+
return $item * 2;
1964+
});
1965+
// Returns all of the unique items in the collection:
1966+
$unique = $collection->unique();
1967+
// Returns a new collection with the keys reset to consecutive integers:
1968+
$values = $collection->values();
1969+
// Filters the collection by a given key / value pair:
1970+
$filtered = $collection->where('price', 100);
1971+
// Merges together the values of the given array with the values of the collection:
1972+
$zipped = $collection->zip([100, 200]);
1973+
</pre>
1974+
</section>
18321975
</div>
18331976
</div>
18341977
</div>

index_zh-CN.html

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,6 +1840,149 @@ <h6>Miscellaneous</h6>
18401840
// 返回给定的数值
18411841
$value = with(new Foo)->work();
18421842
</pre>
1843+
</section>
1844+
<section class="cmd-description grid-item">
1845+
<h4><a name="collection" href="#collection">Collection</a> <a href="https://laravel.com/docs/5.1/collections" title="Collections CLI @ Laravel Docs"><i class="icon-file-text"></i></a></h4>
1846+
<pre class="prettyprint lang-php">
1847+
// 创建集合
1848+
collect([1, 2, 3]);
1849+
// 返回该集合所代表的底层数组:
1850+
$collection->all();
1851+
// 返回集合中所有项目的平均值:
1852+
$collection->avg();
1853+
// 将集合拆成多个给定大小的较小集合:
1854+
$collection->chunk(4);
1855+
// 将多个数组组成的集合折成单一数组集合:
1856+
$collection->collapse();
1857+
// 用来判断该集合是否含有指定的项目:
1858+
$collection->contains('New York');
1859+
// 返回该集合内的项目总数:
1860+
$collection->count();
1861+
// 遍历集合中的项目,并将之传入给定的回调函数:
1862+
$collection = $collection->each(function ($item, $key) {
1863+
});
1864+
// 会创建一个包含第 n 个元素的新集合:
1865+
$collection->every(4);
1866+
// 传递偏移值作为第二个参数:
1867+
$collection->every(4, 1);
1868+
// 返回集合中排除指定键的所有项目:
1869+
$collection->except(['price', 'discount']);
1870+
// 以给定的回调函数筛选集合,只留下那些通过判断测试的项目:
1871+
$filtered = $collection->filter(function ($item) {
1872+
return $item > 2;
1873+
});
1874+
// 返回集合中,第一个通过给定测试的元素:
1875+
collect([1, 2, 3, 4])->first(function ($key, $value) {
1876+
return $value > 2;
1877+
});
1878+
// 将多维集合转为一维集合:
1879+
$flattened = $collection->flatten();
1880+
// 将集合中的键和对应的数值进行互换:
1881+
$flipped = $collection->flip();
1882+
// 以键自集合移除掉一个项目:
1883+
$collection->forget('name');
1884+
// 返回含有可以用来在给定页码显示项目的新集合:
1885+
$chunk = $collection->forPage(2, 3);
1886+
// 返回给定键的项目。如果该键不存在,则返回 null:
1887+
$value = $collection->get('name');
1888+
// 根据给定的键替集合内的项目分组:
1889+
$grouped = $collection->groupBy('account_id');
1890+
// 用来确认集合中是否含有给定的键:
1891+
$collection->has('email');
1892+
// 用来连接集合中的项目
1893+
$collection->implode('product', ', ');
1894+
// 移除任何给定数组或集合内所没有的数值:
1895+
$intersect = $collection->intersect(['Desk', 'Chair', 'Bookcase']);
1896+
// 假如集合是空的,isEmpty 方法会返回 true:
1897+
collect([])->isEmpty();
1898+
// 以给定键的值作为集合项目的键:
1899+
$keyed = $collection->keyBy('product_id');
1900+
// 传入回调函数,该函数会返回集合的键的值:
1901+
$keyed = $collection->keyBy(function ($item) {
1902+
return strtoupper($item['product_id']);
1903+
});
1904+
// 返回该集合所有的键:
1905+
$keys = $collection->keys();
1906+
// 返回集合中,最后一个通过给定测试的元素:
1907+
$collection->last();
1908+
// 遍历整个集合并将每一个数值传入给定的回调函数:
1909+
$multiplied = $collection->map(function ($item, $key) {
1910+
return $item * 2;
1911+
});
1912+
// 返回给定键的最大值:
1913+
$max = collect([['foo' => 10], ['foo' => 20]])->max('foo');
1914+
$max = collect([1, 2, 3, 4, 5])->max();
1915+
// 将给定的数组合并进集合:
1916+
$merged = $collection->merge(['price' => 100, 'discount' => false]);
1917+
// 返回给定键的最小值:
1918+
$min = collect([['foo' => 10], ['foo' => 20]])->min('foo');
1919+
$min = collect([1, 2, 3, 4, 5])->min();
1920+
// 返回集合中指定键的所有项目:
1921+
$filtered = $collection->only(['product_id', 'name']);
1922+
// 获取所有集合中给定键的值:
1923+
$plucked = $collection->pluck('name');
1924+
// 移除并返回集合最后一个项目:
1925+
$collection->pop();
1926+
// 在集合前面增加一个项目:
1927+
$collection->prepend(0);
1928+
// 传递第二个参数来设置前置项目的键:
1929+
$collection->prepend(0, 'zero');
1930+
// 以键从集合中移除并返回一个项目:
1931+
$collection->pull('name');
1932+
// 附加一个项目到集合后面:
1933+
$collection->push(5);
1934+
// put 在集合内设置一个给定键和数值:
1935+
$collection->put('price', 100);
1936+
// 从集合中随机返回一个项目:
1937+
$collection->random();
1938+
// 传入一个整数到 random。如果该整数大于 1,则会返回一个集合:
1939+
$random = $collection->random(3);
1940+
// 会将每次迭代的结果传入到下一次迭代:
1941+
$total = $collection->reduce(function ($carry, $item) {
1942+
return $carry + $item;
1943+
});
1944+
// 以给定的回调函数筛选集合:
1945+
$filtered = $collection->reject(function ($item) {
1946+
return $item > 2;
1947+
});
1948+
// 反转集合内项目的顺序:
1949+
$reversed = $collection->reverse();
1950+
// 在集合内搜索给定的数值并返回找到的键:
1951+
$collection->search(4);
1952+
// 移除并返回集合的第一个项目:
1953+
$collection->shift();
1954+
// 随机排序集合的项目:
1955+
$shuffled = $collection->shuffle();
1956+
// 返回集合从给定索引开始的一部分切片:
1957+
$slice = $collection->slice(4);
1958+
// 对集合排序:
1959+
$sorted = $collection->sort();
1960+
// 以给定的键排序集合:
1961+
$sorted = $collection->sortBy('price');
1962+
// 移除并返回从指定的索引开始的一小切片项目:
1963+
$chunk = $collection->splice(2);
1964+
// 返回集合内所有项目的总和:
1965+
collect([1, 2, 3, 4, 5])->sum();
1966+
// 返回有着指定数量项目的集合:
1967+
$chunk = $collection->take(3);
1968+
// 将集合转换成纯 PHP 数组:
1969+
$collection->toArray();
1970+
// 将集合转换成 JSON:
1971+
$collection->toJson();
1972+
// 遍历集合并对集合内每一个项目调用给定的回调函数:
1973+
$collection->transform(function ($item, $key) {
1974+
return $item * 2;
1975+
});
1976+
// 返回集合中所有唯一的项目:
1977+
$unique = $collection->unique();
1978+
// 返回键重设为连续整数的的新集合:
1979+
$values = $collection->values();
1980+
// 以一对给定的键/数值筛选集合:
1981+
$filtered = $collection->where('price', 100);
1982+
// 将集合与给定数组同样索引的值合并在一起:
1983+
$zipped = $collection->zip([100, 200]);
1984+
</pre>
1985+
</section>
18431986
</div>
18441987
</div>
18451988
</div>

0 commit comments

Comments
 (0)