Tfe

Ongi etorri tfe-ren webgunera...

Blog/jokoak_euskaraz/csvToJson.php

(Deskargatu)
<?php

if(count($argv)<3 || !file_exists($argv[1]) || !file_exists($argv[2]))
{
    die($argv[0]." : Missing filename.csv filename.json argument.\n");
}

$newFile = preg_replace("/\.[^\.]+$/","",$argv[1])."-euskaraz.csv";
$newFile_fp = fopen($newFile, 'w');

$json = json_decode(file_get_contents($argv[2]),true);

$newCsv=[];
$trads=[];
if (($handle = fopen($argv[1], "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $trads[$data[0]] = $data[2];
    }
    fclose($handle);
}
foreach($json['strings']['Array'] as &$item)
{
    $key = $item["m_sTag"];
    if(!isset($trads[$key]))
    {
        exit("ERROR $key not found in CSV.");
    }
    $item["m_sText"] = $trads[$key];
}

file_put_contents("New.json", json_encode($json, JSON_PRETTY_PRINT));

print "FILE New.json created.\n";