PHP code example of loilo / collection

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

    

loilo / collection example snippets


$c = Collection::make([ 10, 20, 30, 40 ]);
$c->insertAfter(20, [ 24, 25, 26 ]);
$c->all() === [ 10, 20, 24, 25, 26, 30, 40 ];

$c = Collection::make([
    'name' => 'loilo/collection',
    'version' => '1.0.0'
]);

$c->insertAfter(function ($value, $key) {
    return $key === 'name';
}, [ 'description' => "An extended version of Laravel's collections" ]);

$c->all() === [
    'name' => 'loilo/collection',
    'description' => "An extended version of Laravel's collections",
    'version' => '1.0.0'
];

$c = Collection::make([ 1, 2, 3 ])->extract([ 0, 1 ])
$c->all() === [ 1, 2 ];

$c = Collection::make([
    'a' => [
        'd' => 3,
        'e' => 4
    ],
    'b' => [
        'd' => 5,
        'e' => 6
    ],
    'c' => [
        'd' => 7,
        'e' => 8
    ]
]);

$c->extract([ 'a' ])->all() === [
    'a' => [
        'd' => 3,
        'e' => 4
    ]
];

$c->extract([ '*' => [ 'd' ] ])->all() === [
    'a' => [ 'd' => 3 ],
    'b' => [ 'd' => 5 ],
    'c' => [ 'd' => 7 ]
];

Collection::make([ 'a', 'b', 'c' ]);
    ->rearrange([ 2, 0, 1 ])
    ->all() === [ 'c', 'a', 'b' ];

Collection::make([ 'a', 'b', 'c' ]);
    ->rearrange([ 1 ])
    ->all() === [ 'b', 'a', 'c' ];

Collection::make([ 'a', 'b', 'c' ]);
    ->rearrange(
        [ 'b', 'a', 'c' ],
        Collection::UNARRANGEABLE_APPEND,
        function ($value, $key) {
            return $value;
        }
    ),
    ->all() === [ 'b', 'a', 'c' ];