共计 564 个字符,预计需要花费 2 分钟才能阅读完成。
比如一个数组里面中文值在使用 json_encode 时候不希望把中文转成 u4f60 这种形式,在 PHP5.4 版本中 json_encode 增加了一个 flag 叫JSON_UNESCAPED_UNICODE
$array = array("1" => "PHP code tester Sandbox Online",
"foo" => "bar", 5 , 5 => 89009,
"case" => "Random Stuff:" . rand(100,999),
"PHP Version" => phpversion(),
"你好"
);
echo json_encode($array, JSON_UNESCAPED_UNICODE);
print:
{"1":"PHP code tester Sandbox Online","foo":"bar","2":5,"5":89009,"case":"Random Stuff: 772","PHP Version":"7.1.0","6":"你好"}
不使用 JSON_UNESCAPED_UNICODE 结果:
{"1":"PHP code tester Sandbox Online","foo":"bar","2":5,"5":89009,"case":"Random Stuff: 173","PHP Version":"7.1.0","6":"\u4f60\u597d"}
正文完