WordPress hwk Codes Convertir du JSON au Format CSV

Convertir du JSON au Format CSV

8 May 2018
<?php
function hwk_json_to_csv($json, $delimiter = ';'){
$data = json_decode($json, true);
if(!is_array($data) || empty($data))
return;
$ouput = fopen('php://output', 'w');
$firstline = false;
foreach($data as $line){
if(empty($firstline)){
$firstline = array_keys($line);
fputcsv($ouput, $firstline, $delimiter);
$firstline = array_flip($firstline);
}
fputcsv($ouput, array_merge($firstline, $line), $delimiter);
}
}