59

Elements of an array containing special characters are converted to empty strings when encoding the array with json_encode:

$arr = array ( "funds" => "ComStage STOXX®Europe 600 Techn NR ETF", "time"=>....);
$json = json_encode($arr);

After JSON encoding the element [funds] is null. It happens only with special characters (copyright, trademark etc) like the ones in "ComStage STOXX®Europe 600 Techn NR ETF".

Any suggestions?

Thanks

UPDATE: This is what solved the problem prior to populating the array (all names are taken from the db):

$mysqli->query("SET NAMES 'utf8'");
user2723490
  • 2,010
  • 4
  • 27
  • 37
  • 1
    Not all special characters are UTF8. The very ASCII "greather than" and "lower than" can create havoc in a json-encoded string. I just had the case today. htmlentities() or htmlenspecialchars(), although doing their job, didn't provide a solution. For now I had to replace them with plus and minus characters, to avoid FF from interpreting them as HTML tag opening and closing characters. Any other solution welcome. – Fabien Haddadi Apr 10 '18 at 04:46
  • @FabienHaddadi I found this article helpful: https://www.pontikis.net/blog/how-to-escape-json-special-characters-using-php – Sohail Jun 12 '22 at 17:32

9 Answers9

51

The manual for json_encode specifies this:

All string data must be UTF-8 encoded.

Thus, try array_mapping utf8_encode() to your array before you encode it:

$arr = array_map('utf8_encode', $arr);
$json = json_encode($arr);

// {"funds":"ComStage STOXX\u00c2\u00aeEurope 600 Techn NR ETF"}

For reference, take a look at the differences between the three examples on this fiddle. The first doesn't use character encoding, the second uses htmlentities and the third uses utf8_encode - they all return different results.

For consistency, you should use utf8_encode().

Docs

scrowler
  • 24,273
  • 9
  • 60
  • 92
21

Your input has to be encoded as UTF-8 or ISO-8859-1.

http://www.php.net/manual/en/function.json-encode.php

Because if you try to convert an array of non-utf8 characters you'll be getting 0 as return value.


Since 5.5.0 The return value on failure was changed from null string to FALSE.

deW1
  • 5,562
  • 10
  • 38
  • 54
  • On php.net is written: `All string data must be UTF-8 encoded.`. – bitWorking Dec 20 '13 at 00:35
  • 6
    I've tried cleaning the string to conform to UTF-8 without any success. What worked for me - setting MySQL Names to UTF-8 prior to populating the array: $mysqli->query("SET NAMES 'utf8'"); Now all special characters are displayed perfectly fine. – user2723490 Dec 20 '13 at 00:57
  • you might want to use `PDO` instead of `mysqli` btw. imho it offers more functionality :) – deW1 Dec 20 '13 at 01:17
  • 1
    @user2723490 Your suggestion solved the problem for me. Trying to `utf_encode` an accented e was giving me the word "Array" as my result, but getting it on the way out from the database end worked just fine. Thanks! – DiMono May 08 '16 at 18:21
  • Brothers im facing the issue please check my question. https://stackoverflow.com/questions/49998897/php-json-encode-is-returning-0-zero-value-sometimes – Muhammad Ateek Apr 24 '18 at 11:32
  • Please give me such characters which will return 0 on json_encode – Muhammad Ateek Apr 24 '18 at 11:33
  • @MuhammadAteek since 5.5.0 it will return false instead – deW1 May 07 '18 at 17:43
13

To me, it works this way:

# Creating the ARRAY from Result.
$array=array();

while($row = $result->fetch_array(MYSQL_ASSOC))
{
    # Converting each column to UTF8
    $row = array_map('utf8_encode', $row);
    array_push($array,$row);
}

json_encode($array);
sandolkakos
  • 177
  • 1
  • 5
5

you should use this code:

$json = json_encode(array_map('utf8_encode', $arr))

array_map function converts special characters in UTF8 standard

user3711086
  • 75
  • 1
  • 2
5

Use the below function.

function utf8_converter($array)
{
    array_walk_recursive($array, function (&$item, $key) {
        if (!mb_detect_encoding($item, 'utf-8', true)) {
                $item = utf8_encode($item);
        }
    });

    return $array;
}
Lakin Mohapatra
  • 1,097
  • 11
  • 22
3

To avoid escaping of special characters, I'm passing the flag:

JSON_UNESCAPED_UNICODE

Like this:

json_encode($array, JSON_UNESCAPED_UNICODE)
aumanets
  • 3,703
  • 8
  • 39
  • 59
0

Use this code mysql_set_charset("UTF8", $connection);

Example $connection = mysql_connect(DB_HOST_GATEWAY,DB_USER_GATEWAY, DB_PASSWORD_GATEWAY); mysql_set_charset("UTF8", $connection); mysql_select_db(DB_DATABASE_GATEWAY,$connection);

Padmanaban
  • 71
  • 1
  • 5
-3

you should add charset=UTF-8 in meta tag and use json_encode for special characters

$json = json_encode($arr);

json_encode function converts special characters in UTF8 standard

-4

To fix the special character issue you just have to do 2 things

1.mysql_set_charset('utf8'); // set this line on top of your page in which you are using json.

  1. If you are saving json data in database make sure that the particular column collation is set to "latin1_swedish_ci".
Musakkhir Sayyed
  • 7,012
  • 13
  • 42
  • 65
kavita
  • 1