PHP code example of mleczek / collections
1. Go to this page and download the library: Download mleczek/collections 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/ */
mleczek / collections example snippets
$collection = new \Mleczek\Collections\Collection([
['id' => 3, 'firstName' => 'Debra', 'lastName' => 'Barnett'],
['id' => 8, 'firstName' => 'Ronnie', 'lastName' => 'Coleman'],
['id' => 2, 'firstName' => 'Gabriel', 'lastName' => 'Adams'],
]);
// ...and perform some operations:
$names = $collection
->whereIn(fn($x) => $x['id'], [3, 2])
->map(fn($x) => $x['firstName'] .' '. $x['lastName'])
->join(', ');
// $names is equal "Debra Barnett, Gabriel Adams"
$sum = collection([1, 2, 5])
->map(fn($x) => $x * 2)
->sum();
// $sum is equal 16 (2+4+10)
$collection = collection([2, 3])->addFirst(1); // [1, 2, 3]
$collection = collection([2, 3])->addLast(4); // [2, 3, 4]
$avg = collection([1, 2, 6])->avg(); // 3
$items = [
['value' => 3],
['value' => 7],
];
$avg = collection($items)
->avg(fn($item, $key) => $item['value']); // 5
$collection = collection([1, 2, 3])->chunk(2); // [[1, 2], [3]]
$count = collection([1, 2])->count(); // 2
collection(['a' => 1, 'b' => 2])
->each(fn($item, $key) => printf("$key:$item"));
$key = collection(['a' => 1, 'b' => 2])->firstKey(); // 'a'
$item = collection(['a' => 1, 'b' => 2])->first(); // 1
$collection = collection([[1, 2], [3]])->flatten(); // [1, 2, 3]
$items = [
['brand' => 'Jeep', 'model' => 'Cherokee Latitude'],
['brand' => 'Nissan', 'model' => 'Sentra SV'],
['brand' => 'Nissan', 'model' => 'Murano Platinum'],
];
$collection = collection($items)
->groupBy(fn($item, $key) => $item['brand']); // ['Jeep' => [...], 'Nissan' => [...]]
$test = collection([2, 7, 3])
->has(fn($item, $key) => $item === 7); // true
$test = collection([1, 4])->isAssociative(); // false
$test = collection(['a' => 1, 4])->isAssociative(); // true
$test = collection([])->isEmpty(); // true
$test = collection([1, 4])->isIndexed(); // true
$test = collection(['a' => 1, 4])->isIndexed(); // false
$test = collection([8, 2])->isNotEmpty(); // true
$string = collection(['Nissan', 'Jeep', 'Ford'])->join(', '); // 'Nissan, Jeep, Ford'
$items = [
['id' => 5, 'username' => 'lorraine'],
['id' => 1, 'username' => 'gabriel.hill'],
['id' => 4, 'username' => 'steward'],
];
$collection = collection($items)
->keyBy(fn($item, $key) => $item['id']); // [5 => [...], 1 => [...], 4 => [...]]
$array = collection(['a' => 1, 'b' => 3])->keys(); // ['a', 'b']
$key = collection(['a' => 1, 'b' => 2])->lastKey(); // 'b'
$item = collection(['a' => 1, 'b' => 2])->last(); // 2
$collection = collection([1, 4])
->map(fn($item, $key) => $item * 2); // [2, 8]
$max = collection([1, 4, 7])->max(); // 7
$items = [
['value' => 3],
['value' => 8],
];
$avg = collection($items)
->max(fn($item, $key) => $item['value']); // 8
$collection = collection([1, 2])->merge([3, 4]); // [1, 2, 3, 4]
$first = collection([1, 2]);
$second = collection(['a' => 1, 'b' => 2]);
$collection = $first->merge($second);// [1, 2, 'a' => 1, 'b' => 2]
$max = collection([1, 4, 7])->min(); // 1
$items = [
['value' => 3],
['value' => 8],
];
$avg = collection($items)
->min(fn($item, $key) => $item['value']); // 3
$item = collection(['a' => 1, 'b' => 2])->randomKey();
$item = collection([1, 8, 4])->random();
$initialState = 2;
$result = collection([1, 8, 4])
->reduce(fn($item, $state) => $state + $item, $initialState); // 15
$collection = collection([1, 8, 4])->removeFirst(); // [8, 4]
$collection = collection([1, 8, 4])->removeFirst(2); // [4]
$collection = collection([1, 8, 4])->removeLast(); // [1, 8]
$collection = collection([1, 8, 4])->removeLast(2); // [1]
$collection = collection([1, 8, 4])->reverse(); // [4, 8, 1]
$collection = collection([1, 8, 4])->skip(); // [8, 4]
$collection = collection([1, 8, 4])->skip(2); // [4]
$collection = collection([1, 8, 4])->sortDesc(); // [8, 4, 1]
$items = [
['value' => 3],
['value' => 7],
];
$collection = collection($items)
->sortDesc(fn($item) => $item['value']); // ['value' => 7, 'value' => 3]
$collection = collection([1, 8, 4])->sort(); // [1, 4, 8]
$items = [
['value' => 3],
['value' => 7],
];
$collection = collection($items)
->sort(fn($item) => $item['value']); // ['value' => 3, 'value' => 7]
$sum = collection([1, 2, 6])->sum(); // 9
$items = [
['value' => 3],
['value' => 7],
];
$sum = collection($items)
->sum(fn($item, $key) => $item['value']); // 10
$collection = collection([1, 8, 4])->take(); // [1]
$collection = collection([1, 8, 4])->take(2); // [1, 8]
$array = collection([6, 3, 1])->toArray(); // [6, 3, 1]
$collection = collection([6, 1, 3, 1])->unique(); // [6, 1, 3]
$items = [
['brand' => 'Jeep', 'model' => 'Cherokee Latitude'],
['brand' => 'Nissan', 'model' => 'Sentra SV'],
['brand' => 'Nissan', 'model' => 'Murano Platinum'],
];
$collection = collection([$items])
->unique(fn($item, $key) => $item['brand']);
$values = collection(['a' => 1, 'b' => 2])->values(); // [1, 2]
$collection = collection([8, 4, 2])
->whereIn(fn($item, $key) => $item, [4, 7, 2]); // [4, 2]
$collection = collection([8, 4, 2])
->where(fn($item, $key) => $item > 3); // [8, 4]