Why would json_encode return an empty string

I have a simple php structure with 3 nested arrays.

I do not use particular objects and I build myself the arrays with 2 nested loops.

Here is a sample of the var_dump of the array I want to convert to Json.

array (size=2)
  'tram B' => 
    array (size=2)
      0 => 
        array (size=3)
          'name' => string 'Ile Verte' (length=9)
          'distance' => int 298
          'stationID' => int 762
      1 => 
        array (size=3)
          'name' => string 'La Tronche Hôpital' (length=18)
          'distance' => int 425
          'stationID' => int 771
  16 => 
    array (size=4)
      0 => 
        array (size=3)
          'name' => string 'Bastille' (length=8)
          'distance' => int 531
          'stationID' => int 397
      1 => 
        array (size=3)
          'name' => string 'Xavier Jouvin' (length=13)
          'distance' => int 589
          'stationID' => int 438

In another script I have a similar structure and json_encode works fine. So I don't understand why json_encode won't work here.

Edit : there seems to be a problem with the encoding. When mb_detect_encoding returns ASCII, the json_encode works but when it returns UTF8, it doesn't work anymore.

Edit2 : json_last_error() returns JSON_ERROR_UTF8 which means : Malformed UTF-8 characters, possibly incorrectly encoded.


Solution 1:

Well after 2 hours of digging (cf Edits)

I found out following :

  • In my case it's a encoding problem
  • mb_detect_encoding returns probably a faulty response, some strings were probably not UTF-8
  • using utf8_encode() on those string solved my problem, but see note below

Here is a recursive function that can force convert to UTF-8 all the strings contained in an array:

function utf8ize($d) {
    if (is_array($d)) {
        foreach ($d as $k => $v) {
            $d[$k] = utf8ize($v);
        }
    } else if (is_string ($d)) {
        return utf8_encode($d);
    }
    return $d;
}

Use it simply like this:

echo json_encode(utf8ize($data));

Note: utf8_encode() encodes ISO-8859-1 string to UTF-8 as per the docs so if you are unsure of the input encoding iconv() or mb_convert_encoding() may be better options as noted in comments and other solutions.

Solution 2:

Matthieu Riegler presented really good solution however I had to slightly modify it to handle objects too:

function utf8ize($d) {
    if (is_array($d)) 
        foreach ($d as $k => $v) 
            $d[$k] = utf8ize($v);

     else if(is_object($d))
        foreach ($d as $k => $v) 
            $d->$k = utf8ize($v);

     else 
        return utf8_encode($d);

    return $d;
}

One more note: json_last_error() may be helpful in debugging json_encode()/json_encode() functions.

Solution 3:

For me, the answer to this problem was setting charset=utf8 in my PDO connection.

$dbo = new PDO('mysql:host=localhost;dbname=yourdb;charset=utf8', $username, $password);

Solution 4:

Adam Bubela also presented really good solution who helped me solved my problem, and here is the simplified function :

function utf8ize($d)
{ 
    if (is_array($d) || is_object($d))
        foreach ($d as &$v) $v = utf8ize($v);
    else
        return utf8_encode($d);

    return $d;
}

Solution 5:

I have exactly the same problem on PHP 5.6. I use Open Server + Nginx on Windows 7. All charsets are set to UTF-8. In theory, according the official documentation, flag

JSON_UNESCAPED_UNICODE

should solve this. Unfortunately this is not my case. I do not know, why. All snippets above do not solve my problem, thus I have found my own implementation. I believe it could help someone. At least, Russian letters pass the test.

function utf8ize($d) {
    if (is_array($d) || is_object($d)) {
        foreach ($d as &$v) $v = utf8ize($v);
    } else {
        $enc   = mb_detect_encoding($d);

        $value = iconv($enc, 'UTF-8', $d);
        return $value;
    }

    return $d;
}