PHP code example of simsoft / data-flow
1. Go to this page and download the library: Download simsoft/data-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/ */
simsoft / data-flow example snippets
use Simsoft\DataFlow\DataFlow;
(new DataFlow())
->from([1, 2, 3])
->transform(function($num) {
return $num * 2;
})
->load(function($num) {
echo $num . PHP_EOL;
})
->run();
// Output:
// 2
// 4
// 6
use Simsoft\DataFlow\DataFlow;
(new DataFlow())
->from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
->transform(function($num) {
return $num * 2;
})
->limit(5) // output only 5 data.
->load(function($num) {
echo $num . PHP_EOL;
})
->run();
// Output:
// 2
// 4
// 6
// 8
// 10
use Simsoft\DataFlow\DataFlow;
(new DataFlow())
->from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
->filter(function($num) {
// The call back should return bool.
// In this case, return even number only.
return $num % 2 === 0;
})
->load(function($num) {
echo $num . PHP_EOL;
})
->run();
// Output:
// 2
// 4
// 6
// 8
// 10
(new DataFlow())
->from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
->chunk(3) // set chunk size
->load(function(array $chunk, $key) {
echo $key . '=' . json_encode($chunk, JSON_THROW_ON_ERROR) . PHP_EOL;
})
->run();
// Output:
// 0=[1,2,3]
// 1=[4,5,6]
// 2=[7,8,9]
// 3=[10]
(new DataFlow())
->from([
['First Name' => 'John', 'Last Name' => 'Doe', 'age' => 20],
['First Name' => 'Jane', 'Last Name' => 'Doe', 'age' => 30],
['First Name' => 'John', 'Last Name' => 'Smith', 'age' => 50],
['First Name' => 'Jane', 'Last Name' => 'Smith', 'age' => 60],
])
->map([
// rename the key
'first_name' => 'First Name',
'last_name' => 'Last Name',
// customise data via callback method.
'full_name' => fn($data) => $data['first_name'] . ' ' . $data['last_name'],
'senior' => fn($data) => $data['age'] > 30 ? 'Yes' : 'No',
])
->load(function($data) {
echo $data['full_name'] . ' is ' . $data['age'] . ' years old. ' . $data['senior'] . PHP_EOL;
})
->run();
// Output:
// John Doe is 20 years old. No
// Jane Doe is 30 years old. Yes
// John Smith is 50 years old. Yes
// Jane Smith is 60 years old. Yes
$flow1 = (new DataFlow())
->from([1, 2, 3])
->transform(function($num) {
return $num * 2;
});
(new DataFlow())
->from($flow1) // connect flow1 to flow2.
->transform(function($num) {
return $num * 3;
})
->load(function($num) {
echo $num . PHP_EOL;
})
->run();
// Output:
// 6
// 12
// 18