php json_encode() show#39;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"}]
我相信这是因为我的数据库中的文本包含 ( ' ) 标记.尝试了各种stripslashes或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 而不是文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:php json_encode() 显示 null 而不是文本


基础教程推荐
- 超薄框架REST服务两次获得输出 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在多维数组中查找最大值 2021-01-01