PHP code example of loophp / collection

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

    

loophp / collection example snippets


  

  $input = ['foo', 'bar', 'baz'];

  // Using the Collection library
  $collection = Collection::fromIterable($input)
      ->filter(static fn(string $userId): bool => 'foo' !== $userId)
      ->reverse();

  foreach ($collection as $item); // ['baz','bar']

  // Using single operations.
  $pipe = Pipe::of()(
    Reverse::of(),
    Filter::of()($filterCallback)
  );

  foreach ($pipe($input) as $item); // ['baz','bar']
  

  

  $input = ['foo,bar', 'baz,john'];

  $flatMap = static fn (callable $callback) =>
    Pipe::of()(
        Map::of()(static fn(string $name): array => explode(',', $name)),
        Flatten::of()(1)
    );

  foreach ($flatMap($input) as $item); // ['foo', 'bar', 'baz', 'john']