json_encode 是 PHP 中对变量进行 JSON 编码的函数。通常使用是将数组转换成 json 格式。
但今天在对这个数组进行转换的过程中出现了一点小问题:
$data = [
'single_select' => [
'title' => '三角形的内角和为____',
'options' [
'A' => 180,
'B' => 90,
'C' => 360,
'D' => 270,
]
]
];
json_encode($data) 的打印结果如下:
{
"single_select" : {
"title" : "\u4e09\u89d2\u5f62\u7684\u5185\u89d2\u548c\u4e3a____",
"options" : {
"A" : 180
"B" : 90,
"C" : 360,
"D" : 270
}
}
}
可以看到,中文被转移成了 Unicode 字符,但是没有以字面编码来编译。通过查看文档,很快找了解决方法:
json_encode($data, JSON_UNESCAPED_UNICODE)