json_encode 数组 php

json_encode of array php

我有以下数组数组:

array(1) {
  [0]=>
  array(2) {
    [0]=>
    array(2) {
      [0]=>
      string(3) "abc"
      [1]=>
      string(3) "įāē"
    }
    [1]=>
    array(2) {
      [0]=>
      string(3) "čaē"
      [1]=>
      string(3) "qwe"
    }
  }
}

我正在使用下面的代码在页面上回显结果:

echo json_encode($array);

我在我的页面上得到以下结果:

[[["abc",null],[null,"qwe"]]]

每个带有特殊字符的字符串都被转换为空。 所以我在数组中的每个元素上尝试了 utf8_encode

foreach($array as &$subarray1){
    foreach($subarray1 as &$subarray2){
        foreach($subarray2 as &$subarray3){
            $subarray3 = utf8_encode($subarray3);
        }
    }
}

但我得到以下结果:

[[["abc","\u00e1\u00e2\u00e7"],["\u00e8a\u00e7","qwe"]]]

正确的编码方式是什么?

json_encode supports a second parameter so you can use the constant JSON_UNESCAPED_UNICODE 如下所示:

$arr = [
    0 => [0 => "abc", 1 => "įāē"],
    1 => [0 => "čaē", 1 => "qwe"]
];

echo json_encode($arr, JSON_UNESCAPED_UNICODE);

You can find a working demo here: https://ideone.com/J5bvT5