PHP code example of xayan / dataflow

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

    

xayan / dataflow example snippets



DataFlow\Data\Entity;
use DataFlow\IO\CSV\Export;
use DataFlow\IO\CSV\ExportPolicy;
use DataFlow\IO\CSV\Import;
use DataFlow\IO\CSV\ImportPolicy;

$file = 'your_file.csv';

$header = fgetcsv(fopen($file, 'r'));

// This will create ImportPolicy that will import all available columns, using file header as a reference:
$importPolicy = ImportPolicy::fromHeader($header);

$collection = Import::fromFile($file, $importPolicy);

$newCollection = $collection->filter(function(Entity $entity) {
    // Select only entities that contain the 'age' property and its value is >= 18
    return $entity->has('age') && $entity->get('age') >= 18;
})->map(function(Entity $entity) {
    // Set a new property
    $entity->set('name', $entity->get('firstName') . ' ' . $entity->get('lastName'));

    // Remove properties
    $entity->remove('firstName');
    $entity->remove('lastName');

    // Remember to return it
    return $entity;
});

// Create ExportPolicy automatically from an entity, so all data will be exported
$exportPolicy = ExportPolicy::fromEntity($newCollection->get(0));

// Create a new file and write export contents to it
$outputFile = fopen('another_file.csv', 'w');
Export::toStream($outputFile, $newCollection, $exportPolicy);