这篇文章中
https://andi.cn/page/621468.html
介绍了使用 php 将数组编码成 json 格式的字符串并保存到文件中。
本文,对其代码稍修改,介绍使用 json_encode 时添加参数 JSON_PRETTY_PRINT 来得到美化(格式化)的 json 字符串。
<pre> <?php $a = [ 'x' => 1, 1, [1,2,3,] ]; print_r($a); $s = json_encode($a, JSON_PRETTY_PRINT); echo $s; file_put_contents('data/w.json', $s);