laravel 中如何写json文件
2019-09-20 tech laravel 1 mins 581 字

写json要注意使用如下方法:
$content = stripslashes(json_encode($array));
stripslashes用于删除反斜杠。
写文件使用laravel方法
- Storage::put($file, $content);
 
或php原生方法:
- file_put_contents
 
    // 读文件
    $jsonString = file_get_contents(base_path('resources/lang/en.json'));
    $data = json_decode($jsonString, true);
    // 更新内容
    $data['country.title'] = "Change Manage Country";
    // 写文件
    $newJsonString = json_encode($data, JSON_PRETTY_PRINT);
    file_put_contents(base_path('resources/lang/en.json'), stripslashes($newJsonString));