目次

php でjsonのread and write

phpでのjsonファイル読み込みと書き出しの超速レシピ用メモ

Write

$obj = '{ "name": "馬場哲晃",  "gender": "男"}';
$data = json_decode($obj,true);
 
$json = fopen('sample.json', 'w+b');
fwrite($json, json_encode($data));
fclose($json);

Read

$json_string = file_get_contents("sample.json");    //文字列データとしてjsonファイルを読み込み。
$json_string_encoded = mb_convert_encoding($json_string, 'UTF8', 'ASCII,JIS,UTF-8,EUC-JP,SJIS-WIN');
$json_decoded = json_decode($json_string_encoded,true);

Reference