PHP code example of izica / php-collection
1. Go to this page and download the library: Download izica/php-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' );
izica / php-collection example snippets
$collection = PhpCollection:collect([100 , 200 , 300 , 400 ]);
$collection2 = $collection->filter(function ($item) {
return $item > 200 ;
})
$products = [
["id" => 1 , "name" => "product 1" , "price" => 100 ],
["id" => 2 , "name" => "product 2" , "price" => 200 ],
["id" => 3 , "name" => "product 3" , "price" => 300 ]
];
$collection = PhpCollection:collect($products);
$collection = PhpCollection:collect($products[0 ], true );
$collection = PhpCollection:collect([100 , "data" , 300 , 400 ])->implode();
$products = [
["id" => 1 , "name" => "product 1" , "price" => 100 ],
["id" => 2 , "name" => "product 2" , "price" => 200 ],
["id" => 3 , "name" => "product 3" , "price" => 300 ]
];
$serializer = function ($item) {
return "{$item['name']}-{$item['price']}$" ;
};
$collection = PhpCollection:collect($products)->implode(", " , $serializer);
$products = [
["id" => 1 , "name" => "product 1" , "price" => 100 ],
["id" => 2 , "name" => "product 2" , "price" => 200 ],
["id" => 3 , "name" => "product 3" , "price" => 300 ]
];
$collection = PhpCollection:collect($products)->pluck("id" )->all();
$collection = PhpCollection:collect($products)->pluck("name" )->all();
$products = [
["id" => 1 , "name" => "product 1" , "price" => 100 ],
["id" => 2 , "name" => "product 2" , "price" => 200 ],
["id" => 3 , "name" => "product 3" , "price" => 300 ]
];
$collection = PhpCollection:collect($products)->only(["id" , "name" ])->all();
$collection = PhpCollection:collect($products)->only(["id" , "name" => "title" ])->all();
$products = [
["id" => 1 , "name" => "product 1" , "price" => 100 ],
["id" => 2 , "name" => "product 2" , "price" => 200 ],
["id" => 3 , "name" => "product 3" , "price" => 300 ]
];
$collection = PhpCollection:collect($products)->exclude(["name" ])->all();
$products = [
["id" => 1 , "name" => "product 1" , "price" => 100 ],
["id" => 2 , "name" => "product 2" , "price" => 200 ],
["id" => 3 , "name" => "product 3" , "price" => 300 ]
];
$collection = PhpCollection:collect($products)
->filter(function ($item) {
return $item["price" ] > 100
})
->all();
$products = [
["id" => 1 , "name" => "product 1" , "price" => 100 ],
["id" => 2 , "name" => "product 2" , "price" => 200 ],
["id" => 3 , "name" => "product 3" , "price" => 300 ]
];
$collection = PhpCollection:collect($products)
->map(function ($item) {
$item["pricex2" ] = $item["price" ] * 2 ;
return $item;
})
->all();
$products = [
["id" => 16 , "name" => "product 1" , "price" => 100 ],
["id" => 22 , "name" => "product 2" , "price" => 200 ],
["id" => 31 , "name" => "product 3" , "price" => 300 ]
];
$collection = PhpCollection:collect($products)->keyBy("id" )->all();
$products = [
["id" => 16 , "category_id" => 1 , "name" => "product 1" , "price" => 100 ],
["id" => 22 , "category_id" => 2 , "name" => "product 2" , "price" => 200 ],
["id" => 31 , "category_id" => 2 , "name" => "product 3" , "price" => 300 ]
];
$collection = PhpCollection:collect($products)->groupBy("category_id" )->all();