PHP json_encode从unicode字符返回null值

时间:2013-02-14 05:00:07

标签: php json

我有以下数组:

Array
(
    [BookDateID] => 4
    [HotelName] => Adams’ Inn
)

输出:

{"BookDateID":"4","HotelName":null}

任何魔法?

BTW,我有一个替代解决方案,通过循环每个数组并让它们mb_convert_encoding(str,'HTML-ENTITIES')但我希望该字符保持不变,因为我必须将其插入DB。

foreach($array as $key=>$value){
    $array[$key] = mb_convert_encoding($value,'HTML-ENTITIES');
}

1 个答案:

答案 0 :(得分:2)

json_encode需要UTF-8编码数据。确保数据是UTF-8编码的;目前它可能是Latin-1编码的。如何做到这一点取决于数据来自何处。阅读What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With TextHandling Unicode Front To Back In A Web App