PHP code example of imran / collection

1. Go to this page and download the library: Download imran/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/ */

    

imran / collection example snippets


$collection = new Collection(['John', 'doe', null]);
$collection->map(function (string $name) { 
      return strtoupper($name);
  })->reject(function (string $name) {
      return empty($name);
  });

$collection = new Collection([1, 2, 3]);

use Imran\Collection\Collection;
Collection::macro('toUpper', function () {
    return $this->map(function (string $value) {
        return strtoupper($value);
    });
});
$collection = new Collection(['first', 'second']);
$upper = $collection->toUpper();
// ['FIRST', 'SECOND']

use Imran\Collection\Collection;
Collection::macro('toLocale', function (string $locale) { 
    return $this->map(function (string $value) use ($locale) {
        return getLocale($value, [], $locale);    
    });
});
$collection = new Collection(['first', 'second']);
$translated = $collection->toLocale('es');

$collection = new Collection([1, 2, 3]);
$collection->all(); // [1, 2, 3]

$collection = new Collection([
    ['foo' => 10],
    ['foo' => 10],
    ['foo' => 20],
    ['foo' => 40]
]);
$average = $collection->avg('foo');
// 20 
$collection = new Collection([1, 1, 2, 4]);
$average = $collection->avg();
// 2

$collection = new Collection([1, 2, 3, 4, 5, 6, 7]);
$chunks = $collection->chunk(4);
$chunks->all();
// [[1, 2, 3, 4], [5, 6, 7]]

$collection = new Collection(str_split('AABBCCCD'));
$chunks = $collection->chunkWhile(function (string $value, int $key, Collection $chunk) {
    return $value === $chunk->last();
});
$chunks->all();
// [['A', 'A'], ['B', 'B'], ['C', 'C', 'C'], ['D']]

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

$collectionA = new Collection([1, 2, 3]);
$collectionB = $collectionA->collect();
$collectionB->all();
// [1, 2, 3]

$collection = new Collection(['name', 'age']);
$combined = $collection->combine(['George', 29]);
$combined->all();
// ['name' => 'George', 'age' => 29]

$collection = new Collection(['John Doe']);
$concatenated = $collection->concat(['Jane Doe'])
                ->concat(['name' => 'Johnny Doe']);
$concatenated->all();
// ['John Doe', 'Jane Doe', 'Johnny Doe']

$collection = new Collection([1, 2, 3, 4, 5]);
$collection->contains(function (int $value, int $key) {
    return $value > 5;
});
// false

$collection = new Collection(['name' => 'Desk', 'price' => 100]);
$collection->contains('Desk');
// true
$collection->contains('New York');
// false

$collection = new Collection([
    ['product' => 'Desk', 'price' => 200],
    ['product' => 'Chair', 'price' => 100],
]);
$collection->contains('product', 'Bookcase');
// false

$collection = new Collection([])
$collection->containsOneItem();
// false
$collection = new Collection(['1'])
$collection->containsOneItem();
// true
$collection = new Collection(['1', '2'])
$collection->containsOneItem();
// false

$collection = new Collection([1, 2, 3, 4]);
$collection->count();
// 4

$collection = new Collection([1, 2, 2, 2, 3]);
$counted = $collection->countBy();
$counted->all();
// [1 => 1, 2 => 3, 3 => 1]

$collection = new Collection(['[email protected]', '[email protected]', '[email protected]']);
$counted = $collection->countBy(function (string $email) {
    return substr(strrchr($email, "@"), 1);
});
$counted->all();
// ['gmail.com' => 2, 'yahoo.com' => 1]

$collection = new Collection([1, 2]);
$matrix = $collection->crossJoin(['a', 'b']);
$matrix->all();
/*
    [
        [1, 'a'],
        [1, 'b'],
        [2, 'a'],
        [2, 'b'],
    ]
*/

$collection = new Collection([1, 2, 3, 4, 5]);
$diff = $collection->diff([2, 4, 6, 8]);
$diff->all();
// [1, 3, 5]

$collection = new Collection([
    'color' => 'orange',
    'type' => 'fruit',
    'remain' => 6,
]);
$diff = $collection->diffAssoc([
    'color' => 'yellow',
    'type' => 'fruit',
    'remain' => 3,
    'used' => 6,
]);
$diff->all();
// ['color' => 'orange', 'remain' => 6]

$collection = new Collection([
    '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]

$collection = new Collection([1, 2, 3, 4, 5]);
$collection->doesntContain(function (int $value, int $key) {    
    return $value < 5;
});
// false

$collection = new Collection(['name' => 'Desk', 'price' => 100]);
$collection->doesntContain('Table');
// true
$collection->doesntContain('Desk');
// false

$collection = new Collection([
    ['product' => 'Desk', 'price' => 200],
    ['product' => 'Chair', 'price' => 100],
]);
$collection->doesntContain('product', 'Bookcase');
// true

$collection = new Collection(['a', 'b', 'a', 'c', 'b']);
$collection->duplicates();
// [2 => 'a', 4 => 'b']

$employees = new Collection([
    ['email' => '[email protected]', 'position' => 'Developer'],
    ['email' => '[email protected]', 'position' => 'Designer'],
    ['email' => '[email protected]', 'position' => 'Developer'],
]);
$employees->duplicates('position');
// [2 => 'Developer']

$collection = new Collection([1, 2, 3, 4]);
$collection->each(function (int $item, int $key) {
    // ...
});

$collection->each(function (int $item, int $key) {
    if (/* condition */) {
         return false;
    }
});

$collection = new Collection([['John Doe', 35], ['Jane Doe', 33]]);
$collection->eachSpread(function (string $name, int $age) {
    // ...
});

$collection->eachSpread(function (string $name, int $age) {
    return false;
});

$collection = new Collection([1, 2, 3, 4]);
$collection->every(function (int $value, int $key) {
    return $value > 2;
});
// false

$collection = new Collection([]);
$collection->every(function (int $value, int $key) {
    return $value > 2;
});
// true

$collection = new Collection(['product_id' => 1, 'price' => 100, 'discount' => false]);
$filtered = $collection->except(['price', 'discount']);
$filtered->all();
// ['product_id' => 1]

$collection = new Collection([1, 2, 3, 4]);
$filtered = $collection->filter(function (int $value, int $key) { 
   return $value > 2;
});
$filtered->all();
// [3, 4]

$collection = new Collection([1, 2, 3, null, false, '', 0, []]);
$collection->filter()->all();
// [1, 2, 3]

$collection = new Collection([1, 2, 3, 4]);
$colleciton->first(function (int $value, int $key) {
    return $value > 2;
});
// 3

$collection = new Collection([1, 2, 3, 4]);
$collection->first();
// 1

$collection = new Collection([1, 2, 3, 4]);
$collection->firstOrFail(function (int $value, int $key) {
    return $value > 5;
});
// Throws Exception...

$collection = new Collection([]);
$collection->firstOrFail();
// Throws Exception...

$collection = new Collection([
    ['name' => 'Regena', 'age' => null],
    ['name' => 'Linda', 'age' => 14],
    ['name' => 'Diego', 'age' => 23],
    ['name' => 'Linda', 'age' => 84],
]);
$collection->firstWhere('name', 'Linda');
// ['name' => 'Linda', 'age' => 14]

$collection->firstWhere('age', '>=', 18);
// ['name' => 'Diego', 'age' => 23]

$collection->firstWhere('age');
// ['name' => 'Linda', 'age' => 14]

$collection = new Collection([
    ['name' => 'Sally'],
    ['school' => 'Arkansas'],
    ['age' => 28]
]);
$flattened = $collection->flatMap(function (array $values) {
    return array_map('strtoupper', $values);
});
$flattened->all();
// ['name' => 'SALLY', 'school' => 'ARKANSAS', 'age' => '28'];

$collection = new Collection([
    'name' => 'john',
    'languages' => [
        'php', 'javascript'
    ]
]);
$flattened = $collection->flatten();
$flattened->all();
// ['john', 'php', 'javascript'];

$collection = new Collection([
    'Apple' => [
        [
            'name' => 'iPhone 6S',
            'brand' => 'Apple'
        ],
    ],
    'Samsung' => [
        [
            'name' => 'Galaxy S7',
            'brand' => 'Samsung'
        ],
    ],
]);
$products = $collection->flatten(1);
$products->values()->all();
/*
    [
        ['name' => 'iPhone 6S', 'brand' => 'Apple'],
        ['name' => 'Galaxy S7', 'brand' => 'Samsung'],
    ]
*/

$collection = new Collection(['name' => 'john', 'framework' => 'laravel']);
$flipped = $collection->flip();
$flipped->all();
// ['john' => 'name', 'laravel' => 'framework']

$collection = new Collection(['name' => 'john', 'framework' => 'laravel']);
$collection->forget('name');
$collection->all();
// ['framework' => 'laravel']

$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(['name' => 'john', 'framework' => 'codeigniter']);
$value = $collection->get('name');
// john

$collection = new Collection(['name' => 'john', 'framework' => 'codeigniter']);
$value = $collection->get('age', 34);
// 34

$collection->get('email', function () {
    return '[email protected]';
});
// [email protected]

$collection = new Collection([
    ['account_id' => 'account-x10', 'product' => 'Chair'],
    ['account_id' => 'account-x10', 'product' => 'Bookcase'],
    ['account_id' => 'account-x11', 'product' => 'Desk'],
]);
$grouped = $collection->groupBy('account_id');
$grouped->all();
/*
    [
        'account-x10' => [
            ['account_id' => 'account-x10', 'product' => 'Chair'],
            ['account_id' => 'account-x10', 'product' => 'Bookcase'],
        ],
        'account-x11' => [
            ['account_id' => 'account-x11', 'product' => 'Desk'],
        ],
    ]
*/

$grouped = $collection->groupBy(function (array $item, int $key) {
   return substr($item['account_id'], -3);
});
$grouped->all();
/*
    [
        'x10' => [
            ['account_id' => 'account-x10', 'product' => 'Chair'],
            ['account_id' => 'account-x10', 'product' => 'Bookcase'],
        ],
        'x11' => [
            ['account_id' => 'account-x11', 'product' => 'Desk'],
        ],
    ]
*/

$data = new Collection([
    10 => ['user' => 1, 'skill' => 1, 'roles' => ['Role_1', 'Role_3']],
    20 => ['user' => 2, 'skill' => 1, 'roles' => ['Role_1', 'Role_2']],
    30 => ['user' => 3, 'skill' => 2, 'roles' => ['Role_1']],
    40 => ['user' => 4, 'skill' => 2, 'roles' => ['Role_2']],
]);
$result = $data->groupBy(['skill', function (array $item) {
    return $item['roles'];
    }],
    preserveKeys: true
);
/*
[
    1 => [
        'Role_1' => [
            10 => ['user' => 1, 'skill' => 1, 'roles' => ['Role_1', 'Role_3']],
            20 => ['user' => 2, 'skill' => 1, 'roles' => ['Role_1', 'Role_2']],
        ],
        'Role_2' => [
            20 => ['user' => 2, 'skill' => 1, 'roles' => ['Role_1', 'Role_2']],
        ],
        'Role_3' => [
            10 => ['user' => 1, 'skill' => 1, 'roles' => ['Role_1', 'Role_3']],
        ],
    ],
    2 => [
        'Role_1' => [
            30 => ['user' => 3, 'skill' => 2, 'roles' => ['Role_1']],
        ],
        'Role_2' => [
            40 => ['user' => 4, 'skill' => 2, 'roles' => ['Role_2']],
        ],
    ],
];
*/

$collection = new Collection(['account_id' => 1, 'product' => 'Desk', 'amount' => 5]);
$collection->has('product');
// true
$collection->has(['product', 'amount']);
// true
$collection->has(['amount', 'price']);
// false

$collection = new Collection(['account_id' => 1, 'product' => 'Desk', 'amount' => 5]);
$collection->hasAny(['product', 'price']);
// true
$collection->hasAny(['name', 'price']);
// false

$collection = new Collection([
    ['account_id' => 1, 'product' => 'Desk'],
    ['account_id' => 2, 'product' => 'Chair'],
]);
$collection->implode('product', ', ');
// Desk, Chair

$collection = new Collection([1, 2, 3, 4, 5]);
$collection->implode('-');
// '1-2-3-4-5'

$collection->implode(function (array $item, int $key) {
        return strtoupper($item['product']);
    }, ', ');
// DESK, CHAIR

$collection = new Collection(['Desk', 'Sofa', 'Chair']);
$intersect = $collection->intersect(['Desk', 'Chair', 'Bookcase']);
$intersect->all();
// [0 => 'Desk', 2 => 'Chair']

$collection = new Collection([
    'serial' => 'UX301', 'type' => 'screen', 'year' => 2009,
]);
$intersect = $collection->intersectByKeys([
    'reference' => 'UX404', 'type' => 'tab', 'year' => 2011,
]);
$intersect->all();
// ['type' => 'screen', 'year' => 2009]

$collection = new Collection([]);
$collection->isEmpty();
// true

$collection = new Collection([]);
$collection->isNotEmpty();
// false

$collection = new Collection(['a', 'b', 'c']);
$collection->join(', '); 
// 'a, b, c'
$collection = new Collection(['a', 'b', 'c']);
$collection->join(', ', ', and '); 
// 'a, b, and c'
$collection = new Collection(['a', 'b']);
$collection->join(', ', ' and '); 
// 'a and b'
$collection = new Collection(['a']);
$collection->join(', ', ' and '); 
// 'a'
$collection = new Collection([]);
$collection->join(', ', ' and '); 
// ''

$collection = new Collection([
    ['product_id' => 'prod-100', 'name' => 'Desk'],
    ['product_id' => 'prod-200', 'name' => 'Chair'],
]);
$keyed = $collection->keyBy('product_id');
$keyed->all();
/*
    [
        'prod-100' => ['product_id' => 'prod-100', 'name' => 'Desk'],
        'prod-200' => ['product_id' => 'prod-200', 'name' => 'Chair'],
    ]
*/

$keyed = $collection->keyBy(function (array $item, int $key) {
    return strtoupper($item['product_id']);
});
$keyed->all();
/*
    [
        'PROD-100' => ['product_id' => 'prod-100', 'name' => 'Desk'],
        'PROD-200' => ['product_id' => 'prod-200', 'name' => 'Chair'],
    ]
*/

$collection = new Collection([
    'prod-100' => ['product_id' => 'prod-100', 'name' => 'Desk'],
    'prod-200' => ['product_id' => 'prod-200', 'name' => 'Chair'],
]);
$keys = $collection->keys();
$keys->all();
// ['prod-100', 'prod-200']

$collection = new Collection([1, 2, 3, 4]);
$collection->last(function (int $value, int $key) {
    return $value < 3;
});
// 2

$collection = new Collection([1, 2, 3, 4]);
$collection->last();
// 4

$collection = new Collection([1, 2, 3, 4, 5]);
$multiplied = $collection->map(function (int $item, int $key) {
    return $item * 2;
});
$multiplied->all();
// [2, 4, 6, 8, 10]

class Currency
{
    /**
     * Create a new currency instance.
     */
    function __construct(
        public string $code
    ) {}
}
$collection = new Collection(['USD', 'EUR', 'GBP']);
$currencies = $collection->mapInto(Currency::class);
$currencies->all();
// [Currency('USD'), Currency('EUR'), Currency('GBP')]

$collection = new Collection([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
$chunks = $collection->chunk(2);
$sequence = $chunks->mapSpread(function (int $even, int $odd) {
    return $even + $odd;
});
$sequence->all();
// [1, 5, 9, 13, 17]

$collection = new Collection([
    [
        'name' => 'John Doe',
        'department' => 'Sales',
    ],
    [
        'name' => 'Jane Doe',
        'department' => 'Sales',
    ],
    [
        'name' => 'Johnny Doe',
        'department' => 'Marketing',
    ]
]);
$grouped = $collection->mapToGroups(function (array $item, int $key) {
    return [$item['department'] => $item['name']];
});
$grouped->all();
/*
    [
        'Sales' => ['John Doe', 'Jane Doe'],
        'Marketing' => ['Johnny Doe'],
    ]
*/
$grouped->get('Sales')->all();
// ['John Doe', 'Jane Doe']

$collection = new Collection([
    [
        'name' => 'John',
        'department' => 'Sales',
        'email' => '[email protected]',
    ],
    [
        'name' => 'Jane',
        'department' => 'Marketing',
        'email' => '[email protected]',
    ]
]);
$keyed = $collection->mapWithKeys(function (array $item, int $key) {
    return [$item['email'] => $item['name']];
});
$keyed->all();
/*
    [
        '[email protected]' => 'John',
        '[email protected]' => 'Jane',
    ]
*/

$max = new Collection([
    ['foo' => 10],
    ['foo' => 20]
]);
$max->max('foo');
// 20
$max = new Collection([1, 2, 3, 4, 5]);
$max->max();
// 5

$median = new Collection([
    ['foo' => 10],
    ['foo' => 10],
    ['foo' => 20],
    ['foo' => 40]
])
$median->median('foo');
// 15
$median = new Collection([1, 1, 2, 4]);
$median->median();
// 1.5

$collection = new Collection(['product_id' => 1, 'price' => 100]);
$merged = $collection->merge(['price' => 200, 'discount' => false]);
$merged->all();
// ['product_id' => 1, 'price' => 200, 'discount' => false]

$collection = new Collection(['Desk', 'Chair']);
$merged = $collection->merge(['Bookcase', 'Door']);
$merged->all();
// ['Desk', 'Chair', 'Bookcase', 'Door']

$collection = new Collection(['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]

$min = new Collection([['foo' => 10], ['foo' => 20]]);
$min->min('foo');
// 10
$min = new Collection([1, 2, 3, 4, 5]);
$min->min();
// 1

$mode = new Collection([
    ['foo' => 10],
    ['foo' => 10],
    ['foo' => 20],
    ['foo' => 40]
]);
$mode->mode('foo');
// [10]
$mode = new Collection([1, 1, 2, 4]);
$mode->mode();
// [1]
$mode = new Collection([1, 1, 2, 2]);
$mode->mode();
// [1, 2]

$collection = new Collection(['a', 'b', 'c', 'd', 'e', 'f']);
$collection->nth(4);
// ['a', 'e']

$collection->nth(4, 1);
// ['b', 'f']

$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(['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']

$collection = new Collection([1, 2, 3, 4, 5, 6]);
[$underThree, $equalOrAboveThree] = $collection->partition(function (int $i) {
    return $i < 3;
});
 
$underThree->all();
// [1, 2]
 
$equalOrAboveThree->all();
 
// [3, 4, 5, 6]

$collection = new Collection([1, 2, 3]);
$piped = $collection->pipe(function (Collection $collection) {
    return $collection->sum();
});
// 6

class ResourceCollection
{
    /**
     * Create a new ResourceCollection instance.
     */
    public function __construct(
      public Collection $collection,
    ) {}
}
$collection = new Collection([1, 2, 3]);
$resource = $collection->pipeInto(ResourceCollection::class);
$resource->collection->all();
// [1, 2, 3]

use Imran\Collection\Collection;
$collection = new Collection([1, 2, 3]);
$result = $collection->pipeThrough([
    function (Collection $collection) {
        return $collection->merge([4, 5]);
    },
    function (Collection $collection) {
        return $collection->sum();
    },
]);
// 15

$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([
    [
        'name' => 'Laracon',
        'speakers' => [
            'first_day' => ['Rosa', 'Judith'],
        ],
    ],
    [
        'name' => 'VueConf',
        'speakers' => [
            'first_day' => ['Abigail', 'Joey'],
        ],
    ],
]);
$plucked = $collection->pluck('speakers.first_day');
$plucked->all();
// [['Rosa', 'Judith'], ['Abigail', 'Joey']]

$collection = new Collection([
    ['brand' => 'Tesla',  'color' => 'red'],
    ['brand' => 'Pagani', 'color' => 'white'],
    ['brand' => 'Tesla',  'color' => 'black'],
    ['brand' => 'Pagani', 'color' => 'orange'],
]);
$plucked = $collection->pluck('color', 'brand');
$plucked->all();
// ['Tesla' => 'black', 'Pagani' => 'orange']

$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->pop(3);
// new Collection([5, 4, 3])
$collection->all();
// [1, 2]

$collection = new Collection([1, 2, 3, 4, 5]);
$collection->prepend(0);
$collection->all();
// [0, 1, 2, 3, 4, 5]

$collection = new Collection(['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']
php
$collection = new Collection([1, 2, 3, 4]);
$collection->push(5);
$collection->all();
// [1, 2, 3, 4, 5]
php
$collection = new Collection([1, 2, 3, 4, 5]);
$collection->random();
// 4 - (retrieved randomly)
php
$random = $collection->random(3);
$random->all();
// [2, 4, 5] - (retrieved randomly)
php
$collection = new Collection();
$collection->range(3, 6);
$collection->all(); // [3, 4, 5, 6]
php
$collection = new Collection([1, 2, 3]);
$total = $collection->reduce(function ($carry, $item) {
    return $carry + $item;
});
// 6
php
$collection->reduce(function ($carry, $item) {
        return $carry + $item;
    }, 
4);
// 10
php
$collection = new Collection([
    'usd' => 1400,
    'gbp' => 1200,
    'eur' => 1000,
]);
$ratio = [
    'usd' => 1,
    'gbp' => 1.37,
    'eur' => 1.22,
];
$collection->reduce(function ($carry, $value, $key) use ($ratio) {
    return $carry + ($value * $ratio[$key]);
});
 
// 4264
php
$collection = new Collection([[1, 2], [3, 4], [5, 6]]);
$result = $collection->reduceSpread(function ($carry, $item, $key) {
          return [$carry + $item[0] + $item[1]];
}, 0);
// [21]
php
$collection = new Collection([1, 2, 3, 4]);
$filtered = $collection->reject(function (int $value, int $key) {
    return $value > 2;
});
$filtered->all();
// [1, 2]
php
$collection = new Collection(['John', 'Abigail', 'James']);
$replaced = $collection->replace([1 => 'Victoria', 3 => 'Finn']);
$replaced->all();
// ['John', 'Victoria', 'James', 'Finn']
php
$collection = new Collection([
    'John',
    'Abigail',
    [
        'James',
        'Victoria',
        'Finn'
    ]
]);
$replaced = $collection->replaceRecursive([
    'Charlie',
    2 => [1 => 'King']
]);
 
$replaced->all();
 
// ['Charlie', 'Abigail', ['James', 'King', 'Finn']]
php
$collection = new Collection([2, 4, 6, 8]);
$collection->search(4);
// 1
php
$collection = new Collection([2, 4, 6, 8]);
$collection->search('4', $strict = true);
// false
php
$collection = new Collection([2, 4, 6, 8]);
$collection->search(function (int $item, int $key) {
    return $item > 5;
});
// 2
php
$collection = new Collection([1, 2, 3, 4, 5]);
$collection->shift();
// 1
$collection->all();
// [2, 3, 4, 5]
php
$collection = new Collection([1, 2, 3, 4, 5]);
$collection->shift(3);
// new Collection([1, 2, 3])
$collection->all();
// [4, 5]
php
$collection = new Collection([1, 2, 3, 4, 5]);
$shuffled = $collection->shuffle();
$shuffled->all();
// [3, 2, 5, 1, 4] - (generated randomly)
php
$collection = new Collection([1, 2, 3, 4]);
$subset = $collection->skipUntil(function (int $item) {
    return $item >= 3;
});
$subset->all();
// [3, 4]
php
$collection = new Collection([1, 2, 3, 4]);
$subset = $collection->skipUntil(3);
$subset->all();
// [3, 4]
php
$collection = new Collection([1, 2, 3, 4]);
$subset = $collection->skipWhile(function (int $item) {
    return $item <= 3;
});
$subset->all();
// [4]
php
$slice = $collection->slice(4, 2);
$slice->all();
// [5, 6]
php
$transactions->sliding(2)->eachSpread(function (Collection $previous, Collection $current) {
    $current->total = $previous->total + $current->amount;
});
php
$collection = new Collection([1, 2, 3, 4, 5]);
$chunks = $collection->sliding(3, step: 2);
$chunks->toArray();
// [[1, 2, 3], [3, 4, 5]]
php
$collection = new Collection([1, 2, 3, 4]);
$collection->sole(function (int $value, int $key) {
    return $value === 2;
});
// 2
php
$collection = new Collection([
    ['product' => 'Desk', 'price' => 200],
    ['product' => 'Chair', 'price' => 100],
]);
$collection->sole('product', 'Chair');
// ['product' => 'Chair', 'price' => 100]
php
$collection = new Collection([
    ['product' => 'Desk', 'price' => 200],
]);
$collection->sole();
// ['product' => 'Desk', 'price' => 200]
php
$collection = new Collection([5, 3, 1, 2, 4]);
$sorted = $collection->sort();
$sorted->values()->all();
// [1, 2, 3, 4, 5]
php
$collection = new Collection([
    ['title' => 'Item 1'],
    ['title' => 'Item 12'],
    ['title' => 'Item 3'],
]);
$sorted = $collection->sortBy('title', SORT_NATURAL);
$sorted->values()->all();
/*
    [
        ['title' => 'Item 1'],
        ['title' => 'Item 3'],
        ['title' => 'Item 12'],
    ]
*/
php
$collection = new Collection([
    ['name' => 'Desk', 'colors' => ['Black', 'Mahogany']],
    ['name' => 'Chair', 'colors' => ['Black']],
    ['name' => 'Bookcase', 'colors' => ['Red', 'Beige', 'Brown']],
]);
$sorted = $collection->sortBy(function (array $product, int $key) {
    return count($product['colors']);
});
 
$sorted->values()->all();
 
/*
    [
        ['name' => 'Chair', 'colors' => ['Black']],
        ['name' => 'Desk', 'colors' => ['Black', 'Mahogany']],
        ['name' => 'Bookcase', 'colors' => ['Red', 'Beige', 'Brown']],
    ]
*/
php
$collection = new Collection([
    ['name' => 'John', 'age' => 34],
    ['name' => 'Abigail Otwell', 'age' => 30],
    ['name' => 'John', 'age' => 36],
    ['name' => 'Abigail Otwell', 'age' => 32],
]);
$sorted = $collection->sortBy([fn (array $a, array $b) => $a['name'] <=> $b['name'],fn (array $a, array $b) => $b['age'] <=> $a['age'],]);
$sorted->values()->all();
/*
    [
        ['name' => 'Abigail Doe', 'age' => 32],
        ['name' => 'Abigail Doe', 'age' => 30],
        ['name' => 'John', 'age' => 36],
        ['name' => 'John', 'age' => 34],
    ]
*/
php
$collection = new Collection([5, 3, 1, 2, 4]);
$sorted = $collection->sortDesc();
$sorted->values()->all();
// [5, 4, 3, 2, 1]
php
$collection = new Collection([
    'id' => 22345,
    'first' => 'John',
    'last' => 'Doe',
]);
$sorted = $collection->sortKeys();
 
$sorted->all();
 
/*
    [
        'first' => 'John',
        'id' => 22345,
        'last' => 'Doe',
    ]
*/
php
$collection = new Collection([
    'ID' => 22345,
    'first' => 'John',
    'last' => 'Doe',
]);
$sorted = $collection->sortKeysUsing('strnatcasecmp');
 
$sorted->all();
 
/*
    [
        'first' => 'John',
        'ID' => 22345,
        'last' => 'Doe',
    ]
*/
php
$collection = new Collection([1, 2, 3, 4, 5]);
$chunk = $collection->splice(2);
$chunk->all();
// [3, 4, 5]
$collection->all();
// [1, 2]
php
$collection = new Collection([1, 2, 3, 4, 5]);
$chunk = $collection->splice(2, 1);
$chunk->all();
// [3]
$collection->all();
// [1, 2, 4, 5]
php
$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]
php
$collection = new Collection([1, 2, 3, 4, 5]);
$groups = $collection->split(3);
$groups->all();
// [[1, 2], [3, 4], [5]]
php
$collection = new Collection([1, 2, 3, 4, 5]);
$collection->sum();
// 15
php
    $collection = new Collection([
    ['name' => 'Chair', 'colors' => ['Black']],
    ['name' => 'Desk', 'colors' => ['Black', 'Mahogany']],
    ['name' => 'Bookcase', 'colors' => ['Red', 'Beige', 'Brown']],
]);
$collection->sum(function (array $product) {
    return count($product['colors']);
});
 
// 6
php
$collection = new Collection([0, 1, 2, 3, 4, 5]);
$chunk = $collection->take(3);
$chunk->all();
// [0, 1, 2]
php
$collection = new Collection([0, 1, 2, 3, 4, 5]);
$chunk = $collection->take(-2);
$chunk->all();
// [4, 5]
php
$collection = new Collection([1, 2, 3, 4]);
$subset = $collection->takeUntil(function (int $item) {
    return $item >= 3;
});
 
$subset->all();
 
// [1, 2]
php
$collection = new Collection([1, 2, 3, 4]);
$subset = $collection->takeUntil(3);
$subset->all();
// [1, 2]
php
$collection = new Collection([1, 2, 3, 4]);
$subset = $collection->takeWhile(function (int $item) {
    return $item < 3;
});
 
$subset->all();
 
// [1, 2]
php
$collection = new Collection([2, 4, 3, 1, 5]);
$collection->sort()
    ->tap(function (Collection $collection) {
        Log::debug('Values after sorting', $collection->values()->all());
    })
    ->shift();
// 1
php
$collection = Collection::times(10, function (int $number) {
    return $number * 9;
});
 
$collection->all();
 
// [9, 18, 27, 36, 45, 54, 63, 72, 81, 90]
php
$collection = new Collection([1, 2, 3, 4, 5]);
$collection->transform(function (int $item, int $key) {
    return $item * 2;
});
 
$collection->all();
 
// [2, 4, 6, 8, 10]
php
$collection = new Collection([1, 1, 2, 2, 3, 4, 2]);
$unique = $collection->unique();
$unique->values()->all();
// [1, 2, 3, 4]
php
$unique = $collection->unique(function (array $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'],
    ]
*/
php
$collection = new Collection([1, 2, 3]);
$collection->u$collection->unless(true, function (Collection $collection) {
    return $collection->push(4);
});
 
$collection->unless(false, function (Collection $collection) {
    return $collection->push(5);
});
 
$collection->all();
 
// [1, 2, 3, 5]
php
$collection = new Collection([1, 2, 3]);
$collection->unless(true, function (Collection $collection) {
    return $collection->push(4);
}, function (Collection $collection) {
    return $collection->push(5);
});
 
$collection->all();
 
// [1, 2, 3, 5]
php
Collection::unwrap(new Collection('John Doe'));
// ['John Doe']
Collection::unwrap(['John Doe']);
// ['John Doe']
Collection::unwrap('John Doe');
// 'John Doe'
php
$collection = new Collection([
    ['product' => 'Desk', 'price' => 200],
    ['product' => 'Speaker', 'price' => 400],
]);
$value = $collection->value('price');
// 200
php
$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],
    ]
*/
php
$collection = new Collection([1, 2, 3]);
$collection->when(true, function (Collection $collection, int $value) {
    return $collection->push(4);
});
 
$collection->when(false, function (Collection $collection, int $value) {
    return $collection->push(5);
});
 
$collection->all();
 
// [1, 2, 3, 4]
php
$collection = new Collection([1, 2, 3]);
$collection->when(false, function (Collection $collection, int $value) {
    return $collection->push(4);
}, function (Collection $collection) {
    return $collection->push(5);
});
 
$collection->all();
 
// [1, 2, 3, 5]
php
$collection = new Collection(['Michael', 'Tom']);
$collection->whenEmpty(function (Collection $collection) {
    return $collection->push('Adam');
});
 
$collection->all();
 
// ['Michael', 'Tom']
 
 
$collection = new Collection();
 
$collection->whenEmpty(function (Collection $collection) {
    return $collection->push('Adam');
});
 
$collection->all();
 
// ['Adam']
php
$collection = new Collection(['Michael', 'Tom']);
$collection->whenEmpty(function (Collection $collection) {
    return $collection->push('Adam');
}, function (Collection $collection) {
    return $collection->push('Taylor');
});
 
$collection->all();
 
// ['Michael', 'Tom', 'Taylor']
php
$collection = new Collection(['michael', 'tom']);
$collection->whenNotEmpty(function (Collection $collection) {
    return $collection->push('adam');
});
 
$collection->all();
 
// ['michael', 'tom', 'adam']
 
 
$collection = collect();
 
$collection->whenNotEmpty(function (Collection $collection) {
    return $collection->push('adam');
});
 
$collection->all();
 
// []
php
$collection = new Collection();
$collection->whenNotEmpty(function (Collection $collection) {
    return $collection->push('adam');
}, function (Collection $collection) {
    return $collection->push('john');
});
 
$collection->all();
 
// ['john']
php
$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],
    ]
*/
php
$collection = new Collection([
    ['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'],
    ]
*/
php
$collection = new Collection([
    ['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],
    ]
*/
php
$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' => 'Desk', 'price' => 200],
        ['product' => 'Bookcase', 'price' => 150],
    ]
*/
php
$collection = new Collection([
    ['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],
    ]
*/
php
$collection = new Collection([
    ['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],
    ]
*/