Created
March 30, 2020 11:17
-
-
Save Matteo182/f23b090f020797f6063dbdb34221e813 to your computer and use it in GitHub Desktop.
converte csv to json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Template Name: from csv to create json | |
* | |
* Template for converte csv to json. | |
* | |
* @package understrap | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; // Exit if accessed directly. | |
} | |
?> | |
<?php | |
$filename = 'file_name_in.csv'; | |
// Array all'interno del quale travaso il contenuto del CSV: | |
$arrayDatiCsvTravasati = []; | |
// certificato utofirmato | |
$context = stream_context_create(array('ssl'=>array( | |
'verify_peer' => false, | |
'cafile' => '/bedrockcall.loacl.crt' | |
))); | |
// Apro il file per la lettura: | |
if (($h = fopen("{$filename}", "rb", false, $context)) !== FALSE) | |
{ | |
// Ogni riga dell'array è estratta e convertita in un array $data | |
// Gli elementi del CSV utilizzano la virgola "," come selettore - nel caso, cambiare | |
while (($data = fgetcsv($h, 10000, ",")) !== FALSE){ | |
// Ogni riga del CSV è inserita nell'array di "travaso": | |
$arrayDatiCsvTravasati[] = $data; | |
} | |
// Chiudo il file: | |
fclose($h); | |
} | |
// Rimuovo la prima riga dell'array (con i nomi delle colonne): | |
unset($arrayDatiCsvTravasati[0]); | |
/* echo '<pre>'; | |
var_dump($arrayDatiCsvTravasati[2541]); | |
echo '</pre>'; | |
die; */ | |
$json = array(); | |
foreach($arrayDatiCsvTravasati as $comitato){ | |
// Verifico se il comitato è attivo: | |
/* if($comitato[16] == '0'){ | |
continue; | |
} */ | |
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
// Nome della regione: | |
$regione = strtoupper($comitato[5]); | |
$nazione = strtoupper($comitato[4]); | |
if(!$regione){ | |
continue; | |
} | |
// DISCRIMINO TRA REGIONI ITALIANE E COMITATI ESTERI | |
if($regione == 'NA' || $nazione != 'ITALIA'){ // COMITATI ALL'ESTERO | |
// Le regioni indicate con 'NA' indicano che il comitato si trova all'estero: | |
$regione = 'ESTERO'; | |
$comitatoEstero = $comitato; | |
// Nome della NAZIONE: | |
$nazione = $comitato[4]; | |
if(!$nazione){ | |
continue; | |
} | |
$json[$regione][$nazione][] = $comitatoEstero; | |
} else { // REGIONI ITALIANE | |
// Aggiungo il nodo della REGIONE, se ancora non è stata aggiunta | |
if ( !array_key_exists($regione, $json) ){ | |
$json[$regione] = array(); | |
} | |
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
// Nome della provincia: | |
$provincia = strtoupper($comitato[6]); | |
if(!$provincia){ | |
continue; | |
} | |
// Aggiungo il nodo della PROVINCIA, se ancora non è stata aggiunta | |
if ( !array_key_exists($provincia, $json[$regione]) ){ | |
$json[$regione][$provincia] = array(); | |
} | |
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
// Nome del comune | |
$comune = strtoupper($comitato[7]); | |
if(!$comune){ | |
continue; | |
} | |
// Aggiungo il nodo del COMUNE, se ancora non è stata aggiunta | |
if ( !array_key_exists( $comune, $json[$regione][$provincia]) ){ | |
$json[$regione][$provincia][$comune] = array(); | |
} | |
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
// Aggiungo il comitato al JSON: | |
if ($comitato[14] == 1){ // inserisco nella mappa solo i coordinatori che si sono tesserati coordinatore_tesserato | |
$json[$regione][$provincia][$comune][] = $comitato; | |
} | |
} | |
} | |
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
/* ORDINE ALFABETICO */ | |
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
// Ordino le regioni: | |
ksort($json); | |
//echo '<pre>'; | |
//var_dump($json); | |
//echo '</pre>'; | |
//die; | |
// Definisco una funzione per ordinare i comitati (questa funzione mi servirà nel foreach, qui in basso) | |
// Devono essere ordinati in base alla chiave 1, che contiene il NOME del comitato: | |
function ordinaComitatiPerNome($a, $b) { | |
return $a[1] > $b[1]; | |
} | |
foreach($json as $key1 => $arrProvinceDellaRegione){ | |
if($key1 == 'ESTERO'){ // COMITATI ALL'ESTERO | |
$arrNazioniEstere = $arrProvinceDellaRegione; | |
ksort($arrNazioniEstere); | |
$json[$key1] = $arrNazioniEstere; | |
foreach($arrNazioniEstere as $key2 => $arrComitatiNazioneEstera){ | |
usort($arrComitatiNazioneEstera, 'ordinaComitatiPerNome'); | |
$json[$key1][$key2] = $arrComitatiNazioneEstera; | |
} | |
} else { // REGIONI ITALIANE | |
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
// Ordino le province: | |
ksort($arrProvinceDellaRegione); | |
$json[$key1] = $arrProvinceDellaRegione; | |
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
// Ordino i comuni: | |
foreach($arrProvinceDellaRegione as $key2 => $arrComuniDellaProvincia){ | |
ksort($arrComuniDellaProvincia); | |
$json[$key1][$key2] = $arrComuniDellaProvincia; | |
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | |
// Ordino i comitati: | |
foreach($arrComuniDellaProvincia as $key3 => $arrComitati){ | |
usort($arrComitati, 'ordinaComitatiPerNome'); | |
$json[$key1][$key2][$key3] = $arrComitati; | |
} | |
} | |
} | |
} | |
$json = json_encode($json); | |
echo '<pre>'; | |
echo '['.$json.']'; | |
echo '</pre>'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment