1

I have the following array of arrays:

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"
    }
  }
}

I am using the bellow code to echo the result on a page:

echo json_encode($array);

I get the following result on my page:

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

Every string with special char is converted to null. So I´ve tried utf8_encode on each of the element in the array:

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

But I get the following result:

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

What is the proper way to encode this?

Roald Nefs
  • 1,302
  • 10
  • 29
Ihidan
  • 419
  • 1
  • 6
  • 20

1 Answers1

3

json_encode supports a second parameter so you can use the constant JSON_UNESCAPED_UNICODE like the following:

$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

Sebastian Brosch
  • 42,106
  • 15
  • 72
  • 87