PHP code example of ruifernandees / csv-json-converter

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

    

ruifernandees / csv-json-converter example snippets




uiF\CsvToJson\FileFacade;

$filePath = __DIR__ . "/users.csv";

if (file_exists($filePath)) {
    $fileFacade = new FileFacade();

    /**
     * Is the line on the CSV file that the keys are (Like name, age, and city)
     */
    $lineOfCsvKeysOnTheFile = 1;

    /**
     * -1 if you want to get all lines of the CSV file (after the keys). 
     * If you want to limit, you can pass any number greater than zero
     * (See the examples below)
     */
    $limitOfLines = 1;

    /**
     * Is the start position after the keys that 
     * you want to consider when converting: 0 is the first position
     * (See the examples below)
     */
    $offset = 0;

    $json = $fileFacade->convertCsvToJson($filePath, $lineOfCsvKeysOnTheFile, $limitOfLines, $offset);
    
    echo $json;
} else {
    echo "The file doesn't exists";
}

$jsonFile = __DIR__ . "/users.json";

$fileOpen = fopen($jsonFile, "w");
fwrite($fileOpen, $json);



uiF\CsvToJson\FileFacade;

$filePath =  __DIR__ . '/users.json';

if (file_exists($filePath)) {
    $fileFacade = new FileFacade();
    $csv = $fileFacade->convertJsonToCsv($filePath);
    
    echo "Result:\n{$csv}";
} else {
    echo "The file doesn't exists";
}

$csvFile = __DIR__ . "/users.csv";

$fileOpen = fopen($csvFile, "w");
fwrite($fileOpen, $csv);