How to convert this array into JSON in PHP

arrays, json, mysql, php

Solution

I am running PHP 5.4.7, for me the following code works flawlessly:

$result = json_encode($result, true);

I know you have already tried that. Leonardo's suggestion also works for me:

$result = json_encode($result, JSON_UNESCAPED_UNICODE);

The issue is that in PHP 5.5.0 `json_encode` requires the strings to be UTF-8.

So.. you will have to pass a valid utf8 string, how to do it depend on what encoding you have your strings in. You are right in thinking you need `utf8_encode` or similar function. You may also want to give a look to `iconv`.

Now the issue with `utf8_encode` is that this function will not work with arrays, for that you need a helper function, such as:

function utf8_encode_recursive ($array)
{
    $result = array();
    foreach ($array as $key => $value)
    {
        if (is_array($value))
        {
            $result[$key] = utf8_encode_recursive($value);
        }
        else if (is_string($value))
        {
            $result[$key] = utf8_encode($value);
        }
        else
        {
            $result[$key] = $value;
        }
    }
    return $result;
}

Note 1: `utf8_encode` only accepts strings in ISO-8859-1. Verify what encoding you are using.

Note 2: `htmlspecialchars` and `htmlentities` will not convert all the characters of your encoding, only those "dangerous" (`htmlspecialchars`) or that have html equivalent named entities (`htmlentities`). For this use case use `mb_encode_numericentity` instead.

Note 3: Both `iconv` and `mb_encode_numericentity` will allow you to specify the encoding of your string. Also they don't work with arrays either, so you will need to write recursive helper functions for them too.

Problem

I read many many many posts on this subject and I tried many solutions and I can't convert this multi-array into a JSON string. This is what I see when I `print_r($result)`: ``` Array ( [profiles] => Array ( [0] => Array ( [ID] => 00000000-0000-0000-0000-000000000001 [UserName] => Administrator GU [Age] => 37 [CityStateCode] => Montréal [OnlineSince] => En ligne depuis 6 heures 39 minutes [IsPaying] => true [LabelOnlineStatus] => En ligne ) [1] => Array ( [ID] => ab3dd04e-5621-11e3-b448-103f0c805f5a [UserName] => Guillaume Le Genie [Age] => 68 [CityStateCode] => Montréal [OnlineSince] => En ligne depuis 1 jour 9 heures [IsPaying] => true [LabelOnlineStatus] => Hors-Ligne ) [2] => Array ( [ID] => 00000000-0000-0000-0000-000000000050 [UserName] => Baby-dragoon [Age] => 25 [CityStateCode] => Québec [OnlineSince] => En ligne depuis 5 jours 6 heures [IsPaying] => true [LabelOnlineStatus] => Hors-Ligne ) ) ) ``` I try this (with and without true parameter): ``` $result = json_encode($result, true); $error = json_last_error_msg(); echo "[ERROR : $error]-----[$result]-----"; ``` And I receive: ``` [ERROR : Malformed UTF-8 characters, possibly incorrectly encoded]-----[]----- ``` When I try this: ``` $result = json_encode(htmlspecialchars(utf8_encode($result))); ``` I receive: Warning: utf8_encode() expects parameter 1 to be string, array given in /Applications/XAMPP/xamppfiles/htdocs/cdn/php/functionsv1.php on line 2839 [ERROR : No error]-----[""]----- When I try this: ``` $result = json_encode(htmlspecialchars($result)); ``` I receive: Warning: htmlspecialchars() expects parameter 1 to be string, array given in /Applications/XAMPP/xamppfiles/htdocs/cdn/php/functionsv1.php on line 2839 [ERROR : No error]-----[null]----- I'm really lost! N.B. You see the language is French so we have a char with accent like éèàô etc... The data provide from MySQL Database and database is set to: ``` mysql_query("SET NAMES 'utf8'"); mysql_query('SET CHARACTER SET utf8'); ```

Original source