PHP code example of elephox / collection
1. Go to this page and download the library: Download elephox/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/ */
elephox / collection example snippets
declare(strict_types=1);
use Elephox\Collection\Enumerable;
$data = [5, 2, 1, 4, 3];
$arr = Enumerable::from($data);
$arr->orderBy(fn (int $item) => $item)
->select(function (int $item) {
echo $item;
});
// output: 12345
$arr->where(fn (int $item) => $item % 2 === 0)
->select(function (int $item) {
echo $item;
});
// output: 24
echo $arr->aggregate(fn (int $acc, int $item) => $acc + $item, 0);
// output: 15