php json_encode()显示null而不是文字 [英] php json_encode() show's null instead of text
本文介绍了php json_encode()显示null而不是文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在从数据库中读取一些希伯来语文本,并尝试json_encode
.
如果我print_r
结果我得到:
I am reading from database with some text in Hebrew and trying to json_encode
it.
if i print_r
the results i get:
Array
(
[0] => Array
(
[value] => 88
[text] => כיתה א'
[parent_id] => 1
[level] => 1
)
[1] => Array
(
[value] => 89
[text] => כיתה ב'
[parent_id] => 1
[level] => 1
)
[2] => Array
(
[value] => 91
[text] => כיתה ג'
[parent_id] => 1
[level] => 1
)
)
而json_encode显示:
while the json_encode shows:
[{"value":"88","text":null,"parent_id":"1","level":"1"},{"value":"89","text":null,"parent_id":"1","level":"1"},{"value":"91","text":null,"parent_id":"1","level":"1"}]
我相信这是因为我在数据库中的文本包含一个(')标记. 尝试了各种反斜杠或real_escape_string组合,都无济于事.
i belive it's because my text from the database contains a ( ' ) mark. tried various combination of stripslashes or real_escape_string none have helped.
推荐答案
json_encode
期望数据中包含字符串编码为UTF-8.
json_encode
expects strings in the data to be encoded as UTF-8.
如果尚未将它们转换为UTF-8:
Convert them to UTF-8 if they aren't already:
$results = array_map(function($r) {
$r['text'] = utf8_encode($r['text']);
return $r;
}, $results);
echo json_encode($results);
这篇关于php json_encode()显示null而不是文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文