PHP code example of ozdemirburak / json-csv

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

    

ozdemirburak / json-csv example snippets


use OzdemirBurak\JsonCsv\File\Json;

// JSON to CSV
$json = new Json(__DIR__ . '/file.json');
// To convert JSON to CSV string
$csvString = $json->convert();
// To set a conversion option then convert JSON to CSV and save
$json->setConversionKey('utf8_encoding', true);
$json->convertAndSave(__DIR__ . '/file.csv');
// To convert JSON to CSV and force download on browser
$json->convertAndDownload();

$csvString = (new Json())->fromString('{"name": "John", "age": 30}')->convert();

use OzdemirBurak\JsonCsv\File\Csv;

// CSV to JSON
$csv = new Csv(__DIR__ . '/file.csv');
$csv->setConversionKey('options', JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
// To convert CSV to JSON string
$jsonString = $csv->convert();
// To convert CSV to JSON and save
$csv->convertAndSave(__DIR__ . '/file.json');
// To convert CSV to JSON and force download on browser
$csv->convertAndDownload();

$jsonString = (new Csv())->fromString("name,age\nJohn,30\n")->convert();