PHP json_encode从Unicode字符返回空值 [英] PHP json_encode returns null value from unicode character
问题描述
我有以下数组:
Array
(
[BookDateID] => 4
[HotelName] => Adams’ Inn
)
输出:
{"BookDateID":"4","HotelName":null}
有魔术吗?
顺便说一句,,我有一个替代解决方案,可以遍历每个数组,并让它们使用mb_convert_encoding(str,'HTML-ENTITIES'),但我希望字符保持不变,因为我必须将其插入数据库.
BTW, I have an alternative solution by looping thru each array and have them mb_convert_encoding(str,'HTML-ENTITIES') but I want the character remains the same as I have to insert this into a DB.
foreach($array as $key=>$value){
$array[$key] = mb_convert_encoding($value,'HTML-ENTITIES');
}
推荐答案
json_encode
需要UTF-8编码的数据.确保数据是UTF-8编码的;目前,它很可能是Latin-1编码的.如何执行此操作取决于数据来自何处.阅读每个程序员绝对肯定要了解与文本配合使用的编码和字符集的内容和在Web应用程序中处理Unicode从头到尾.
json_encode
needs UTF-8 encoded data. Make sure the data is UTF-8 encoded; currently it's likely Latin-1 encoded. How to do this depends on where the data comes from. Read What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text and Handling Unicode Front To Back In A Web App.
这篇关于PHP json_encode从Unicode字符返回空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!