php json_encode()显示为null而不是text

eri*_*hak 12 php text json hebrew

我正在阅读数据库中的一些希伯来语文本并尝试使用json_encode它.如果print_r我得到的结果:

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
        )

)
Run Code Online (Sandbox Code Playgroud)

而json_encode显示:

[{"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"}]
Run Code Online (Sandbox Code Playgroud)

我相信它是因为我的数据库中的文本包含一个(')标记.尝试过各种条带组合或者real_escape_string没有帮助.

phi*_*hag 21

json_encode 期望数据中的字符串被编码为UTF-8.

如果它们尚未转换为UTF-8,请将它们转换为:

$results = array_map(function($r) {
  $r['text'] = utf8_encode($r['text']);
  return $r;
}, $results);
echo json_encode($results);
Run Code Online (Sandbox Code Playgroud)