PHP code example of andileong / collection
1. Go to this page and download the library: Download andileong/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/ */
andileong / collection example snippets
use Andileong\Collection\Collection;
$array = [
[
'id' => 1,
'name' => 'andi fake',
'is_subscribed' => true,
'created_at' => '2021-05-07T09:51:51.000000Z',
],
[
'id' => 2,
'name' => 'Betty Waters',
'is_subscribed' => false,
'created_at' => '2021-04-07T09:51:51.000000Z',
],
[
'id' => 3,
'name' => 'Landen Spinka',
'is_subscribed' => true,
'created_at' => '2021-01-07T09:51:51.000000Z',
],
[
'id' => 4,
'name' => 'Dell Stoltenberg',
'is_subscribed' => false,
'created_at' => '2021-09-07T09:51:51.000000Z',
],
[
'id' => 5,
'name' => 'Guy Moen PhD',
'is_subscribed' => true,
'created_at' => '2021-07-07T09:51:51.000000Z',
],
[
'id' => 6,
'name' => 'Ida Swaniawski',
'is_subscribed' => false,
'created_at' => '2021-05-07T09:51:51.000000Z',
],
[
'id' => 7,
'name' => 'Norberto Baumbach',
'is_subscribed' => false,
'created_at' => '2021-06-07T09:51:51.000000Z',
],
[
'id' => 8,
'name' => 'McDermott',
'is_subscribed' => true,
'created_at' => '2021-08-07T09:51:51.000000Z',
],
[
'id' => 9,
'name' => 'Lavon Nitzsche DDS',
'is_subscribed' => true,
'created_at' => '2021-06-07T09:51:51.000000Z',
],
[
'id' => 10,
'name' => 'Natalie Huel',
'is_subscribed' => true,
'created_at' => '2021-01-07T09:51:51.000000Z',
],
];
//we can do as below chaining call
$collection = Collection::make($array);
$collection = $collection
->filter( fn($user) => $user['is_subscribed'] )
->map( function ($user) {
$user['name'] = strtoupper($user['name']);
return $user;
})
->sortBy('created_at', SORT_DESC)
->limit(3);