PHP code example of eslym / php-light-stream

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

    

eslym / php-light-stream example snippets



Eslym\LightStream\Stream;

$data = [
  ['id'=>1, 'name'=>'Test1', 'val'=>rand(0, 9)],
  ['id'=>2, 'name'=>'Test2', 'val'=>rand(0, 9)],
  ['id'=>3, 'name'=>'Test1', 'val'=>rand(0, 9)],
  ['id'=>4, 'name'=>'Test2', 'val'=>rand(0, 9)],
  ['id'=>5, 'name'=>'Test1', 'val'=>rand(0, 9)],
  ['id'=>6, 'name'=>'Test2', 'val'=>rand(0, 9)],
  ['id'=>7, 'name'=>'Test1', 'val'=>rand(0, 9)],
  ['id'=>8, 'name'=>'Test2', 'val'=>rand(0, 9)]
];
$result = Stream::of($data)
  ->map(function($record){return (object)$record;})
  ->filter(function($record){return $record->val < 5;})
  ->pair(function($record){return $record->name;})
  ->collectWithKeys();
header('Content-Type: text/plain');
var_dump($result);