PHP code example of atomsk / csv-manager

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

    

atomsk / csv-manager example snippets


> 'allowed_extensions' => 'csv,txt'
> 

> 'allowed_extensions' => ['csv', 'txt']
> 

> use CsvManager\Csv;
> 

> use CsvManager\Facades\Csv;
> 

> 'env_config' => 'symfony'
> 

> 'env_config' => 'native' // or 'laravel' if you prefer
> 



return [

    /*
    | Define the environment config.
    | --------------------------------------------------------------
    | You can define a native, laravel or symfony environment config.
    | Warning: Support for the symfony environment has been deprecated.
    */
    'env_config' => 'native',

    /*
    | Define the language of the messages for the use of the library
    | --------------------------------------------------------------
    |
    */
    'language' => 'en',

    /*
    | Define the allowed extensions for files.
    | --------------------------------------------------------------
    |
    */
    'allowed_extensions'    =>  ['csv','txt'],

    /*
    | Whitelist of allowed paths.
    | --------------------------------------------------------------
    | Includes php temp dir and the repository's base path.
    */
    'extra_allowed_paths'   =>  []
];

    use CsvManager\Facades\Csv;
    
    $data = Csv::toArray('my-csv-file.csv');

    use CsvManager\Facades\Csv;
    
    $data = ['foo', 'poo'];
    
    $myCsvPath = Csv::fromArray($data);

    use CsvManager\Facades\Csv;
    
    Csv::toArray('my-csv-file.csv', true, function (array $row)
        {
            // YOUR CODE HERE...
        }
    );

use CsvManager\Core\ConfigManager;
use CsvManager\Core\LanguageManager;
use CsvManager\Integrations\NativeCsv;
use CsvManager\Sources\TrustedFylesystemSource;

// Load configuration (could be from your own array or file)
$config = new ConfigManager([
    'language' => 'en',
    'allowed_extensions' => ['csv', 'txt']
]);

$language   = new LanguageManager($config);
$csv        = new NativeCsv($language);

// Define the source
$source = new TrustedFylesystemSource(
    $config,
    $language,
    __DIR__ . '/storage',
    'example.csv'
);

// Convert array to CSV
$data = [
    ['id' => 1, 'name' => 'John'],
    ['id' => 2, 'name' => 'Jane']
];
$csvPath = $csv->fromArray($data, $source);

echo "CSV created at: " . $csvPath;

// Convert CSV back to array
$result = $csv->toArray($source, true);
print_r($result);

use CsvManager\Core\ConfigManager;
use CsvManager\Core\LanguageManager;
use CsvManager\Integrations\LaravelCsv;
use CsvManager\Sources\TrustedFylesystemSource;

// Load configuration (could be from your own array or file)
$config = new ConfigManager([
    'language' => 'en',
    'allowed_extensions' => ['csv', 'txt']
]);

$language   = new LanguageManager($config);
$csv        = new LaravelCsv($language);

// In Laravel, you can specify a disk if needed
$source = new TrustedFylesystemSource($config, $language, storage_path('app/public'), 'my-laravel.csv', 'public');

// Create CSV
$csv->fromArray([['product' => 'Book', 'price' => 12]], $source);

use CsvManager\Core\ConfigManager;
use CsvManager\Core\LanguageManager;
use CsvManager\Integrations\SymfonyCsv;
use CsvManager\Sources\TrustedFylesystemSource;

// Load configuration (could be from your own array or file)
$config = new ConfigManager([
    'language' => 'en',
    'allowed_extensions' => ['csv', 'txt']
]);

$language   = new LanguageManager($config);
$csv        = new SymfonyCsv($language);

$source = new TrustedFylesystemSource($config, $language, __DIR__, 'example.csv');

$csv->fromArray([['foo' => 'bar']], $source);