flow php

Example: Csv

Topic: Data writing

Description

Write data to CSV file.

composer.json
{
    "name": "flow-php/examples",
    "description": "Flow PHP - Examples",
    "license": "MIT",
    "type": "library",
    "require": {
        "flow-php/etl": "1.x-dev",
        "flow-php/etl-adapter-csv": "1.x-dev"
    }
}
code.php
<?php

declare(strict_types=1);

use function Flow\ETL\Adapter\CSV\to_csv;
use function Flow\ETL\DSL\{data_frame, from_array, overwrite};

require __DIR__ . '/vendor/autoload.php';

data_frame()
    ->read(
        from_array([
            ['id' => 1, 'name' => 'John', 'age' => 30],
            ['id' => 2, 'name' => 'Jane', 'age' => 25],
            ['id' => 3, 'name' => 'Bob', 'age' => 35],
            ['id' => 4, 'name' => 'Alice', 'age' => 28],
            ['id' => 5, 'name' => 'Charlie', 'age' => 32],
        ])
    )
    ->collect()
    ->mode(overwrite())
    ->write(to_csv(__DIR__ . '/output.csv'))
    ->run();

Output

id,name,age
1,John,30
2,Jane,25
3,Bob,35
4,Alice,28
5,Charlie,32

Contributors

Join us on GitHub external resource
scroll back to top
Oops, sorry we have to ask this—but we need your okay to use cookies for page view stats.
It helps us improve this open-source project and keep it awesome!
Care to help out?
Cookies Status: Rejected