PHP code example of hectororm / collection
1. Go to this page and download the library: Download hectororm/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/ */
hectororm / collection example snippets
$collection = new Collection();
$collection = new Collection(['my', 'initial', 'array']);
$collection = new LazyCollection();
$collection = new LazyCollection(['my', 'initial', 'array']);
$collection = Collection::new(['foo', 'bar']);
$collection = Collection::new(['foo', 'bar', 'baz']);
$collection->count(); // Returns `3`
count($collection); // Returns `3`
$collection = new Collection();
$collection->getArrayCopy(); // Returns `[]`
$collection = new Collection(['my', 'initial', new Collection(['array'])]);
$collection->getArrayCopy(); // Returns `['my', 'initial', ['array']]`
$collection = new Collection();
$collection->all(); // Returns `[]`
$collection = new Collection(['my', 'initial', new Collection(['array'])]);
$collection->all(); // Returns `['my', 'initial', new Collection(['array'])]`
Collection::new(['foo', 'bar'])->isEmpty(); // Returns FALSE
Collection::new()->isEmpty(); // Returns TRUE
Collection::new(['foo', 'bar'])->isList(); // Returns TRUE
Collection::new(['foo', 'b' => 'bar'])->isList(); // Returns FALSE
$collection = Collection::new(['foo', 'bar']);
$newCollection = $collection->collect();
$collection = Collection::new(['foo', 'bar']);
$collection = $collection->sort();
$collection->getArrayCopy(); // Returns `['bar', 'foo']`
$collection = Collection::new([
'l' => ['name' => 'Lemon', 'nb' => 1],
'o' => ['name' => 'Orange', 'nb' => 1],
'b1' => ['name' => 'Banana', 'nb' => 5],
'b2' => ['name' => 'Banana', 'nb' => 1],
'a1' => ['name' => 'Apple', 'nb' => 10],
'a2' => ['name' => 'Apple', 'nb' => 1],
]);
$collection = $collection->sort(
fn($item1, $item2) => $item1['name'] <=> $item2['name'],
fn($item1, $item2) => $item1['nb'] <=> $item2['nb'],
);
$collection->getArrayCopy();
// Returns:
// [
// 'a2' => ['name' => 'Apple', 'nb' => 1],
// 'a1' => ['name' => 'Apple', 'nb' => 10],
// 'b2' => ['name' => 'Banana', 'nb' => 1],
// 'b1' => ['name' => 'Banana', 'nb' => 5],
// 'l' => ['name' => 'Lemon', 'nb' => 1],
// 'o' => ['name' => 'Orange', 'nb' => 1],
// ]`
$collection = Collection::new([1, 10, 20, 100]);
$collection = $collection->filter(fn($value) => $value >= 20);
$collection->getArrayCopy(); // Returns `[20, 100]`
$collection = Collection::new([new stdClass(), new SimpleXMLElement()]);
$collection = $collection->filterInstanceOf(stdClass::class);
$collection->getArrayCopy(); // Returns `[object<stdClass>]`
$collection = Collection::new(['foo', 'bar', '1', 1, 'quxx']);
$collection->search(1); // Returns 2
$collection->search(1, true); // Returns 3
$collection->search(fn($value) => str_starts_with($value, 'bar')); // Returns 1
$collection = Collection::new(['foo', 'bar', 'baz']);
$collection = $collection->get(); // Return `'foo'`
$collection = $collection->get(1); // Return `'bar'`
$collection = $collection->get(-1); // Return `'baz'`
$collection = Collection::new(['foo', 'bar', 'baz']);
$collection = $collection->first(); // Return `'foo'`
$collection = $collection->first(fn($value) => str_starts_with('ba', $value)); // Return `'bar'`
$collection = Collection::new(['foo', 'bar', 'baz']);
$collection = $collection->last(); // Return `'baz'`
$collection = $collection->last(fn($value) => str_starts_with('ba', $value)); // Return `'baz'`
$collection = Collection::new(['foo', 'bar', 'baz']);
$collection->slice(0, 2)->getArrayCopy(); // Returns `['foo', 'bar']`
$collection->slice(1)->getArrayCopy(); // Returns `['bar', 'baz']`
$collection->slice(-2, 2)->getArrayCopy(); // Returns `['bar', 'baz']`
$collection->slice(-1)->getArrayCopy(); // Returns `['baz']`
$collection = Collection::new(['foo', 'bar', '2', 'baz']);
$collection->contains('foo'); // Returns `true`
$collection->contains('qux'); // Returns `false`
$collection->contains(2); // Returns `true`
$collection->contains(2, true); // Returns `false`
$collection = Collection::new(['foo', 'bar', 'baz']);
$collection = $collection->chunck(2); // Returns 2 collections
$collection->getArrayCopy(); // Returns `[['foo', 'bar'], ['baz']]`
$collection = Collection::new(['k1' => 'foo', 1 => 'bar', 'k2' => 'baz']);
$collection->keys()->getArrayCopy(); // Returns `['k1', 1, 'k2']`
$collection = Collection::new(['k1' => 'foo', 1 => 'bar', 'k2' => 'baz']);
$collection->keys()->getArrayCopy(); // Returns `['foo', 'bar', 'baz']`
$collection = Collection::new(['k1' => 'foo', 1 => 'foo', 'bar', 'k2' => 'baz']);
$collection->unique()->getArrayCopy(); // Returns `['k1' => 'foo', 'bar', 'k2' => 'baz']`
$collection = Collection::new(['k1' => 'foo', 1 => 'foo', 'bar', 'k2' => 'baz']);
$collection->flip()->getArrayCopy(); // Returns `['foo' => 'k1', 'bar' => 0, 'baz' => 'k2']`
$collection = Collection::new(['k1' => 'foo', 'foo', 'bar', 'k2' => 'baz']);
$collection->reverse()->getArrayCopy(); // Returns `['k2' => 'baz', 0 => 'bar', 1 => 'foo', 'k1' => 'foo']`
$collection->reverse(true)->getArrayCopy(); // Returns `['k2' => 'baz', 1 => 'bar', 0 => 'foo', 'k1' => 'foo']`
$collection = Collection::new([
['k1' => 'foo', 'value' => 'foo value'],
['k1' => 'bar', 'value' => 'bar value'],
['k1' => 'baz', 'value' => 'baz value'],
]);
$collection = $collection->column('k1')->getArrayCopy(); // Returns `['foo', 'bar', 'baz']`
$collection = $collection->column('value', 'k1')->getArrayCopy(); // Returns `['foo' => 'foo value', 'bar' => 'bar value', 'baz' => 'baz value']`
$collection = Collection::new(['foo', 'bar', 'baz']);
$collection = $collection->rand(2)->getArrayCopy(); // Returns 2 values random
$collection = Collection::new([1, 2, 3, 4]);
$collection->sum(); // Returns `10`
$collection = Collection::new([1, 2, 3, 4]);
$collection->avg(); // Returns `2.5`
$collection = Collection::new([1, 3, 5, 7]);
$collection->median(); // Returns `4`
$collection = Collection::new([1, 1, 2, 2, 3, 5]);
$collection->variance(); // Returns `1.888888888889`
$collection = Collection::new([1, 3, 5, 7]);
$collection->deviation(); // Returns `2.2360679775`
$collection = Collection::new([1, 2, 3, 4]);
$collection->reduce(fn($carry, $item) => $carry + $item, 10); // Returns `20`
$collection->reduce(fn($carry, $item) => $carry + $item); // Returns `10`
$collection = Collection::new(['foo', 'bar']);
$collection->append('baz', 'qux')->getArrayCopy(); // Returns `['foo', 'bar', 'baz', 'qux']`
$collection = Collection::new(['foo', 'bar']);
$collection->prepend('baz', 'qux')->getArrayCopy(); // Returns `['baz', 'qux', 'foo', 'bar']`