PHP code example of bzrk / php-stream

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

    

bzrk / php-stream example snippets


$data = [
    (object)["name" => "hallo", "data" => ["a", "b"]],
    (object)["name" => "hallo", "data" => ["a", "b", "H", "g"]],
    (object)["name" => "hallo", "data" => ["a", "b", "d"]],
];

$result = Streams::of($data)
    ->map(fn(object $obj) => (object)["name" => $obj->name, "cnt" => count($obj->data)])
    ->filter(fn(object $obj) => $obj->cnt !== 4)
    ->order(new Comparator(fn(object $a, object $b) => $a->cnt - $b->cnt))
    ->map(fn(object $obj) => "$obj->name - $obj->cnt")
    ->toList();
//Result is
['hallo - 2', 'hallo - 3']

class User { ... }

class UserCollection extends Collection
{
    public function __construct(User ...$data)
    {
        parent::__construct($data);
    }

    public function add(User $value): void 
    {
        parent::addEntry($value);
    }
}   

(new UserCollection(new User("name")))->stream()->map(....)