I have the following CSV :
123;Nom long;Nom_court;Entreprise;rue du lac;21789;Marseille;France
Initialization
$ini_array=array();
const SEPARATOR = ';';
// CSV files columns
$columns = array();
$columns[0]="siret";
$columns[1]="longname";
$columns[2]="shortname";
$columns[3]="type";
$columns[4]="rue";
$columns[5]="zipcode";
$columns[6]="city";
$columns[7]="country";
Read the CSV file
Call the method convert_CSV_to_JSON()
Method convert_CSV_to_JSON()
/**
* Convert CSV content in an JSON array
* Need to be improve for global use
* @param String $csv_data Content of csv file
* @param array $columns Field array of you Excel column
* e.g // Excel column
* $columns[0]="uid";
* $columns[1]="code";
* @param array $jsonHierarchy Field hierarchy
* @return array for json encode
*/
function convert_CSV_to_JSON($csv_data, $columns, $jsonHierarchy){
$json = array();
$Data = str_getcsv($csv_data, "\n"); //parse the rows
foreach($Data as $row_index => $Row) {
$data = str_getcsv($Row, SEPARATOR);
// skip the first row, since it's the headers
if($row_index === 0) {
continue;
}
$addr=array();
foreach ($data as $column_index => $column_value) {
// get the key for each entry
if (isset($columns[$column_index])) {
$label = $columns[$column_index];
if (array_key_exists($label, $jsonHierarchy)) {
$parent = $jsonHierarchy[$label];
$addr[$label]=trim($column_value);
$json[$row_index][$parent]=$addr;
} else {
$json[$row_index][$label] = trim($column_value);
}
}
}
}
return $json;
}
Full code to convert CSV to JSON
<?php
/**
* Read a csv file and produce an JSON output in order to import organisations.
*
* Note :
* - The column order is important and set in the convert_CSV_to_JSON§.
* - csv in UTF8 format.
*
* e.g :
* php convertCSVtoJSON.php > organisationPI.json
* php public/index.php oscar organizationsjson:sync /root/PI/organisationPI.json
*
*/
$ini_array=array();
const SEPARATOR = ';';
// Excel columns
$columns = array();
$columns[0]="siret";
$columns[1]="longname";
$columns[2]="shortname";
$columns[3]="type";
$columns[4]="rue";
$columns[5]="zipcode";
$columns[6]="city";
$columns[7]="country";
/*$columns[0]="uid";
$columns[1]="code";
$columns[2]="shortname";
$columns[3]="longname";
$columns[4]="type";
$columns[5]="address";
$columns[6]="city";
$columns[7]="zipcode";
$columns[8]="country";
$columns[9]="phone";
$columns[10]="url";
$columns[11]="email";
$columns[12]="siret";
$jsonHierarchy = array(
"address1" => "address",
"city" => "address",
"zipcode" => "address",
"country" => "address",
);
//PI_import_pathfile="C:\\tmp\\tmp.csv"
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$ini_array = parse_ini_file("C:\\tmp\\config.ini");
} else {
$ini_array = parse_ini_file("/tmp/config.ini");
}
$file=$ini_array["PI_import_pathfile"];
if( file_exists($file)) {
$csv = file_get_contents($file);
$json = convert_CSV_to_JSON($csv, $columns, $jsonHierarchy);
echo json_encode($json, JSON_PRETTY_PRINT);
} else {
echo "Le fichier d'import CSV n'existe pas !";
}
/**
* Convert CSV content in an JSON array
* Need to be improve for global use
* @param String $csv_data Content of csv file
* @param array $columns Field array of you Excel column
* e.g // Excel column
* $columns[0]="uid";
* $columns[1]="code";
* @param array $jsonHierarchy Field hierarchy
* @return array for json encode
*/
function convert_CSV_to_JSON($csv_data, $columns, $jsonHierarchy){
$json = array();
$Data = str_getcsv($csv_data, "\n"); //parse the rows
foreach($Data as $row_index => $Row) {
$data = str_getcsv($Row, SEPARATOR);
// skip the first row, since it's the headers
if($row_index === 0) {
continue;
}
$addr=array();
foreach ($data as $column_index => $column_value) {
// get the key for each entry
if (isset($columns[$column_index])) {
$label = $columns[$column_index];
if (array_key_exists($label, $jsonHierarchy)) {
$parent = $jsonHierarchy[$label];
$addr[$label]=trim($column_value);
$json[$row_index][$parent]=$addr;
} else {
$json[$row_index][$label] = trim($column_value);
}
//exit(1);
/*switch ($column_index) {
case 5:
$addr["address1"]=trim($column_value);
$json[$row_index]["address"]=$addr;
break;
case 6:
$addr["city"]=trim($column_value);
$json[$row_index]["address"]=$addr;
break;
case 7:
$addr["zipcode"]=trim($column_value);
$json[$row_index]["address"]=$addr;
break;
case 8:
$addr["country"]=trim($column_value);
$json[$row_index]["address"]=$addr;
break;
default:
$json[$row_index][$label] = trim($column_value);
}*/
}
}
}
return $json;
}
Version : PHP 7.3.1
No comments:
Post a Comment