You can see the README in:
CSV/JSON Converter is a modern PHP component which abstracts the CSV to JSON and JSON to CSV conversion routine.
Using Composer
$ composer require ruifernandees/csv-json-converter
This example is in examples/csvToJson.php
Code:
<?php
require __DIR__ . '/../vendor/autoload.php';
use RuiF\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";
}
Input file (users.csv):
name,age,city
Rui,18,Maceió
José,25,São Paulo
name | age | city |
---|---|---|
Rui | 18 | Maceió |
José | 25 | São Paulo |
Output (with limit 1 and offset 0):
[
{
"name": "Rui",
"age": "18",
"city": "Maceió"
}
]
Output (with limit 1 and offset 1):
[
{
"name": "José",
"age": "25",
"city": "São Paulo"
}
]
Output (with default limit and offset, getting all elements of the CSV file):
[
{
"name": "Rui",
"age": "18",
"city": "Maceió"
},
{
"name": "José",
"age": "25",
"city": "São Paulo"
}
]
You can save the JSON file with the following code:
$jsonFile = __DIR__ . "/users.json";
$fileOpen = fopen($jsonFile, "w");
fwrite($fileOpen, $json);
This example is in examples/jsonToCsv.php
Code:
<?php
require __DIR__ . '/../vendor/autoload.php';
use RuiF\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";
}
Input file (users.json):
[
{
"name": "Rui",
"age": "18",
"city": "Maceió"
},
{
"name": "José",
"age": "25",
"city": "São Paulo"
}
]
Output:
name,age,city
Rui,18,Maceió
José,25,São Paulo
name | age | city |
---|---|---|
Rui | 18 | Maceió |
José | 25 | São Paulo |
You can save the CSV file with the following code:
$csvFile = __DIR__ . "/users.csv";
$fileOpen = fopen($csvFile, "w");
fwrite($fileOpen, $csv);
$ composer test
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information