PHP code example of benconda / collection

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

    

benconda / collection example snippets


$collection = Collection::from($myIterable);

use BenConda\Collection\Collection;
// [...]

$collection = Collection::from(range(1, 10))
    ->filter(callback: static fn(int $item): bool => $item > 5)
    ->map(on: fn(int $item): string => "The number is $item");

$collection = Collection::from(range(1, 10))
(
  new Filter(
    callback: fn(int $item): bool => $item >= 5
  )
)
(
  new Map(
    callback: fn(int $item): string => "The number is $item"
  )
);

$first = $collection->first(); // $first = The number is 5

$collection = Collection::from(range(1, 10))
foreach ($collection as $key => $item)
{
  // do what you want
}

$collection->toArray();

use Generator;
use BenConda\Collection\Modifier\ModifierInterface;

/**
 * @template TKey
 * @template TValue
 *
 * @implements ModifierInterface<TKey, TValue>
 */
final class Reindex implements ModifierInterface
{
    /**
     *
     * @param iterable<TKey, TValue> $iterable
     *
     * @return Generator<int, TValue>
     */
    public function __invoke(iterable $iterable): Generator
    {
        foreach ($iterable as $value) {
            yield $value;
        }
    }
}