如何在PHP中将此数组转换为JSON

时间:2013-11-28 00:58:01

标签: php mysql arrays json

我在这个主题上阅读了很多帖子,我尝试了很多解决方案,我无法将这个多数组转换为JSON字符串。这是我在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 
                    ) 
            )      
      )

我尝试这个(有和没有真参数):

$result = json_encode($result, true);
$error = json_last_error_msg();
echo "[ERROR : $error]-----[$result]-----";

我收到了:

[ERROR : Malformed UTF-8 characters, possibly incorrectly encoded]-----[]-----

当我尝试这个时:

$result = json_encode(htmlspecialchars(utf8_encode($result)));

我收到:

  

警告:utf8_encode()要求参数1为字符串,数组在 /clications/XAMPP/xamppfiles/htdocs/cdn/php/functionsv1.php 2839 中给出>
[错误:无错误] ----- [“”] -----

当我尝试这个时:

$result = json_encode(htmlspecialchars($result));

我收到:

  

警告:htmlspecialchars()要求参数1为字符串,在 2839 /Applications/XAMPP/xamppfiles/htdocs/cdn/php/functionsv1.php 中给出数组>
[错误:无错误] ----- [null] -----

我真的输了!

N.B。你看到语言是法语,所以我们有一个像éèàô等口音的字符......

MySQL数据库和数据库提供的数据设置为:

mysql_query("SET NAMES 'utf8'");
mysql_query('SET CHARACTER SET utf8');

2 个答案:

答案 0 :(得分:11)

我正在运行PHP 5.4.7,对我来说,下面的代码完美无缺:

$result = json_encode($result, true);

我知道你已经尝试过了。莱昂纳多的建议对我也有用:

$result = json_encode($result, JSON_UNESCAPED_UNICODE);

问题在于PHP 5.5.0 json_encode要求字符串为UTF-8。


所以......你必须传递一个有效的utf8字符串,该怎么做取决于你的字符串是什么编码。你认为你需要utf8_encode或类似的功能是正确的。您可能还想看看iconv

现在utf8_encode的问题是这个函数不能用于数组,因为你需要一个辅助函数,例如:

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

注1:utf8_encode仅接受ISO-8859-1中的字符串。验证您正在使用的编码。

注意2:htmlspecialcharshtmlentities不会转换您编码的所有字符,只会转换为“危险”(htmlspecialchars)或具有等效命名实体的html({{1} }})。对于此用例,请改用mb_encode_numericentity

注3:iconvmb_encode_numericentity都允许您指定字符串的编码。它们也不能使用数组,所以你也需要为它们编写递归的辅助函数。

答案 1 :(得分:1)

如果您使用的是php版本> 5.4.0你可以使用:

$result = json_encode($result, JSON_UNESCAPED_UNICODE);

请参阅docs