Home > Mobile >  Extract Json data from a countries.csv file on github, and create a seperate array of timezones
Extract Json data from a countries.csv file on github, and create a seperate array of timezones

Time:04-01

enter image description here

CodePudding user response:

Using regexp its not perfect solution, but you can transform timezone data to correct json format using function like this:

public function fixJson(string $str): string {
    return preg_replace(
        '/(?<=(\{|\,))(\w )(?=\:)/',
        '"$2"',
        str_replace("'", '"', $zoneRaw) // may not work properly, if values may contain apostroph symbols, but seems not actual for your case
    );
}

So, use this function:

$this->fixJson($data[$i][14]); // returns json string
json_decode($this->fixJson($data[$i][14])); // returns json decoded array

See usage example here https://sandbox.onlinephpfunctions.com/c/88f21

  • Related