PHP code example of yii2mod / collection
1. Go to this page and download the library: Download yii2mod/collection library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
yii2mod / collection example snippets
$collection = new Collection([1, 2, 3]);
// or via `make` function
$collection = Collection::make([1, 2, 3]);
$collection = new Collection([1, 2, 3]);
$collection->all();
// [1, 2, 3]
$collection = new Collection([1, 2, 3, 4, 5]);
$collection->avg();
// 3
$collection = new Collection([
['id' => 1, 'price' => 150],
['id' => 2, 'price' => 250],
]);
$collection->avg('price');
// 200
$collection = new Collection([1, 2, 3, 4, 5, 6, 7]);
$chunks = $collection->chunk(4);
$chunks->toArray();
// [[1, 2, 3, 4], [5, 6, 7]]
$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]
$collection = new Collection(['name', 'age']);
$combined = $collection->combine(['George', 29]);
$combined->all();
// ['name' => 'George', 'age' => 29]
$collection = new Collection(['city' => 'Alabama', 'country' => 'USA']);
$collection->contains('Alabama');
// true
$collection->contains('New York');
// false
$collection = new Collection([
['city' => 'Alabama'],
['city' => 'New York']
]);
$collection->contains('city', 'New York');
// true
$collection = new Collection([1, 2, 3, 4, 5]);
$collection->count();
// 5
$collection = new Collection([1, 2, 3, 4, 5]);
$diff = $collection->diff([2, 4, 6, 8]);
$diff->all();
// [1, 3, 5]
$collection = $collection->each(function ($item, $key) {
if (/* some condition */) {
return false;
}
});
$collection = new Collection(['a', 'b', 'c', 'd', 'e', 'f']);
$collection->every(4);
// ['a', 'e']
$collection->every(4, 1);
// ['b', 'f']
$collection = new Collection(['id' => 1, 'name' => 'Desk', 'price' => 100, 'discount' => false]);
$filtered = $collection->except(['price', 'discount']);
$filtered->all();
// ['id' => 1, 'name' => 'Desk']
$collection = new Collection([1, 2, 3, 4]);
$filtered = $collection->filter(function ($value, $key) {
return $value > 2;
});
$filtered->all();
// [3, 4]
Collection::make([1, 2, 3, 4])->first(function ($key, $value) {
return $value > 2;
});
// 3
$collection = new Collection([1, 2, 3, 4, 5]);
$collection->first();
// 1
Collection::make([1, 2, 3, 4])->last(function ($key, $value) {
return $value > 2;
});
// 4
$collection = new Collection([1, 2, 3, 4, 5]);
$collection->last();
// 5
$collection = new Collection(['language' => 'java', 'languages' => ['php', 'javascript']]);
$collection->flatten();
// ['java', 'php', 'javascript']
$collection = new Collection(['firstName' => 'Igor', 'lastName' => 'Chepurnoy']);
$collection->flip();
// ['Igor' => 'firstName', 'Chepurnoy' => 'lastName']
$collection = new Collection(['firstName' => 'Igor', 'lastName' => 'Chepurnoy']);
$collection->forget('firstName');
$collection->all();
// ['lastName' => 'Chepurnoy']
$collection = new Collection([1, 2, 3, 4, 5, 6, 7, 8, 9]);
$chunk = $collection->forPage(2, 3);
$chunk->all();
// [4, 5, 6]
$collection = new Collection([
'User' => [
'identity' => [
'id' => 1
]
]
]);
$collection->get('User.identity.id');
// 1
$collection->get('User.identity.email', false);
// false
$collection = new Collection([
['id' => 'id_2', 'name' => 'Bob'],
['id' => 'id_2', 'name' => 'John'],
['id' => 'id_3', 'name' => 'Frank'],
]);
$grouped = $collection->groupBy('id');
$grouped->toArray();
/*
[
'id_2' => [
['id' => 'id_2', 'name' => 'Bob'],
['id' => 'id_2', 'name' => 'John'],
],
'id_3' => [
['id' => 'id_3', 'name' => 'Frank'],
],
]
*/
$grouped = $collection->groupBy(function ($item, $key) {
return substr($item['id'], -2);
});
/*
[
'_2' => [
['id' => 'id_2', 'name' => 'Bob'],
['id' => 'id_2', 'name' => 'John'],
],
'_3' => [
['id' => 'id_3', 'name' => 'Frank'],
],
]
*/
$collection = new Collection(['id' => 1, 'name' => 'Igor']);
$collection->has('id');
// true
$collection->has('email');
// false
$collection = new Collection([
['account_id' => 1, 'name' => 'Ben'],
['account_id' => 2, 'name' => 'Bob'],
]);
$collection->implode('name', ', ');
// Ben, Bob
Collection::make(['Ben', 'Bob'])->implode(' and ')
// Ben and Bob
$collection = new Collection(['php', 'python', 'ruby']);
$intersect = $collection->intersect(['python', 'ruby', 'javascript']);
$intersect->all();
// [1 => 'python', 2 => 'ruby']
$collection = (new Collection([]))->isEmpty();
// true
$collection = (new Collection([]))->isNotEmpty();
// false
$collection = new Collection([
['product_id' => '100', 'name' => 'desk'],
['product_id' => '200', 'name' => 'chair'],
]);
$keyed = $collection->keyBy('product_id');
$keyed->all();
/*
[
'100' => ['product_id' => '100', 'name' => 'desk'],
'200' => ['product_id' => '200', 'name' => 'chair'],
]
*/
$collection = new Collection([
['product_id' => '100', 'name' => 'desk'],
['product_id' => '200', 'name' => 'chair'],
]);
$keyed = $collection->keyBy(function ($item) {
return strtoupper($item['name']);
});
$keyed->all();
/*
[
'DESK' => ['product_id' => '100', 'name' => 'desk'],
'CHAIR' => ['product_id' => '200', 'name' => 'chair'],
]
*/
$collection = new Collection([
'city' => 'New York',
'country' => 'USA'
]);
$collection->keys();
// ['city', 'country']
$collection = new Collection([1, 2, 3, 4, 5]);
$multiplied = $collection->map(function ($item, $key) {
return $item * 2;
});
$multiplied->all();
// [2, 4, 6, 8, 10]
$collection = new Collection([['foo' => 10], ['foo' => 20]]);
$max = $collection->max('foo');
// 20
$collection = new Collection([1, 2, 3, 4, 5]);
$max = $collection->max();
// 5
$collection = new Collection(['product_id' => 1, 'name' => 'Desk']);
$merged = $collection->merge(['price' => 100, 'discount' => false]);
$merged->all();
// ['product_id' => 1, 'name' => 'Desk', 'price' => 100, 'discount' => false]
$collection = new Collection([['foo' => 10], ['foo' => 20]]);
$min = $collection->min('foo');
// 10
$collection = new Collection([1, 2, 3, 4, 5]);
$min = $collection->min();
// 1
$collection = new Collection(['product_id' => 1, 'name' => 'Desk', 'price' => 100, 'discount' => false]);
$filtered = $collection->only(['product_id', 'name']);
$filtered->all();
// ['product_id' => 1, 'name' => 'Desk']
$collection = new Collection([
['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']
$collection = new Collection([1, 2, 3, 4, 5]);
$collection->pop();
// 5
$collection->all();
// [1, 2, 3, 4]
$collection = new Collection([1, 2, 3, 4, 5]);
$collection->prepend(0);
$collection->all();
// [0, 1, 2, 3, 4, 5]
$collection = Collection::make(['one' => 1, 'two' => 2]);
$collection->prepend(0, 'zero');
$collection->all();
// ['zero' => 0, 'one' => 1, 'two', => 2]
$collection = new Collection(['product_id' => 'prod-100', 'name' => 'Desk']);
$collection->pull('name');
// 'Desk'
$collection->all();
// ['product_id' => 'prod-100']
$collection = new Collection([1, 2, 3, 4]);
$collection->push(5);
$collection->all();
// [1, 2, 3, 4, 5]
$collection = new Collection(['product_id' => 1, 'name' => 'Desk']);
$collection->put('price', 100);
$collection->all();
// ['product_id' => 1, 'name' => 'Desk', 'price' => 100]
$collection = new Collection([1, 2, 3, 4, 5]);
$collection->random();
// 4 - (retrieved randomly)
$random = $collection->random(3);
$random->all();
// [2, 4, 5] - (retrieved randomly)
$collection = new Collection([1, 2, 3]);
$total = $collection->reduce(function ($carry, $item) {
return $carry + $item;
});
// 6
$collection->reduce(function ($carry, $item) {
return $carry + $item;
}, 4);
// 10
$collection = new Collection([1, 2, 3, 4]);
$filtered = $collection->reject(function ($value, $key) {
return $value > 2;
});
$filtered->all();
// [1, 2]
$collection = new Collection([1, 2, 3, 4, 5]);
$reversed = $collection->reverse();
$reversed->all();
// [5, 4, 3, 2, 1]
$collection = new Collection([2, 4, 6, 8]);
$collection->search(4);
// 1
$collection->search('4', true);
// false
$collection = new Collection([1, 2, 3, 4, 5]);
$collection->shift();
// 1
$collection->all();
// [2, 3, 4, 5]
$collection = new Collection([1, 2, 3, 4, 5]);
$shuffled = $collection->shuffle();
$shuffled->all();
// [3, 2, 5, 1, 4] // (generated randomly)
$collection = new Collection([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]
$collection = new Collection([5, 3, 1, 2, 4]);
$sorted = $collection->sort();
$sorted->values()->all();
// [1, 2, 3, 4, 5]
$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],
]
*/
$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']],
]
*/
$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]
$collection = new Collection([1, 2, 3]);
$collection->sum();
// 6
$collection = new Collection([
['name' => 'Books', 'countOfProduct' => 100],
['name' => 'Chairs', 'countOfProduct' => 200],
]);
$collection->sum('countOfProduct');
// 300
$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
$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]
$collection = new Collection('name');
$collection->toArray();
/*
['name']
*/
$collection = new Collection([2, 4, 3, 1, 5]);
$result = $collection->sort()
->tap(function ($collection) {
// Values after sorting
var_dump($collection->values()->toArray());
})
->shift();
// 1
$collection = new Collection(['name' => 'Desk', 'price' => 200]);
$collection->toJson();
// '{"name":"Desk","price":200}'
$collection = new Collection([1, 2, 3, 4, 5]);
$collection->transform(function ($item, $key) {
return $item * 2;
});
$collection->all();
// [2, 4, 6, 8, 10]
$collection = new Collection([1, 1, 2, 2, 3, 4, 2]);
$unique = $collection->unique();
$unique->values()->all();
// [1, 2, 3, 4]
$collection = new Collection([
['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'],
]
*/
$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],
]
*/
$collection = new Collection([
['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],
]
*/
$collection = new Collection([
['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],
]
*/
$collection = new Collection(['Chair', 'Desk']);
$zipped = $collection->zip([100, 200]);
$zipped->all();
// [['Chair', 100], ['Desk', 200]]
implode()
merge()
put()