PHP code example of php-kit / flow

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

    

php-kit / flow example snippets


use PhpKit\Flow\Flow;

$topCustomers = Flow::from($orders)
    ->expand(fn ($order) => $order['line_items'])
    ->map(fn ($item) => $item['customer'])
    ->where(fn ($customer) => $customer['active'])
    ->mapAndFilter(function ($customer, &$key) {
        $key = $customer['id'];
        return $customer['total_spent'] > 1000 ? $customer : null;
    })
    ->sort('arsort')
    ->only(10)
    ->all();

foreach (Flow::from([1, 2, 3]) as $value) {
    echo $value;
}

// Flow works seamlessly with SPL functions
$count = iterator_count(Flow::from([1, 2, 3])->where(fn($x) => $x > 1));

// Or with any function expecting a Traversable
function processItems(Traversable $items) {
    foreach ($items as $item) {
        // process each item
    }
}

processItems(Flow::from($data)->map($transformer));

$values = Flow::from(function () {
    for ($i = 1; $i <= 3; $i++) {
        yield $i;
    }
});