PHP code example of luchaninov / csv-writer

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

    

luchaninov / csv-writer example snippets


$items = [
    ['k1' => 'v1_1', 'k2' => 'v1_2', 'k3' => 'v1_3'],
    ['k1' => 'v2_1', 'k2' => 'v2_2', 'k3' => 'v2_3'],
    ['k1' => 'v3_1', 'k2' => 'v3_2', 'k3' => 'v3_3'],
];
$s = (new CsvWriter())->generate($items);
/*
k1,k2,k3
v1_1,v1_2,v1_3
v2_1,v2_2,v2_3
v3_1,v3_2,v3_3
*/

(new \Luchaninov\CsvWriter\CsvWriter())->write($filename, $items);

$items = [
    ['k1' => new \DateTime('2000-01-02 03:04:05'), 'k2' => ['a', 'b'], 'k3' => ['a' => 'b', 'c' => 'd']],
    ['k1' => 1, 'k2' => -2.34, 'k3' => "test\ttest\rtest\ntest"],
    'this_will_be_skipped',
    ['k1' => '"v3_1', 'k2' => new FakeStringable(), 'k3' => new FakeJsonSerializable()],
];
$s = new CsvWriter())->generate($items);
/*
k1,k2,k3
2000-01-02 03:04:05,"[""a"",""b""]","{""a"":""b"",""c"":""d""}"
1,-2.34,test\\ttest\\rtest\\ntest
"""v3_1",fakestringable,{"k":"v"}
*/