json_encode returns an empty string

json_encode returns an empty string

Rate this post

If you are trying to grab some information through a web service in JSON format and if PHP function json_encode returns you an empty string, you need to convert the JSON object to UTF8.

Code Sample:

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

Usage:

json_encode(utf8ize($data))
Close