【问题标题】:How to convert this array into JSON in PHP如何在 PHP 中将此数组转换为 JSON
【发布时间】:2013-12-13 21:22:53
【问题描述】:

我阅读了很多关于这个主题的帖子,我尝试了很多解决方案,但我无法将这个多数组转换为 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 
                    ) 
            )      
      )

我试试这个(带和不带 true 参数):

$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 是字符串,数组在 /Applications/XAMPP/xamppfiles/htdocs/cdn/php/functionsv1.php 中的 2839行>
[错误:没有错误]-----[""]-----

当我尝试这个时:

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

我收到:

警告:htmlspecialchars() 期望参数 1 为字符串,数组在 /Applications/XAMPP/xamppfiles/htdocs/cdn/php/functionsv1.php 中的第 2839 行
[错误:无错误]-----[null]-----

我真的迷路了!

注意你看语言是法语,所以我们有一个带有 éèàô 等口音的字符......

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

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

【问题讨论】:

  • 你说是$result2,但你正在尝试使用$result
  • 抱歉 2 是打字错误!我编辑我的帖子!

标签: php mysql arrays json


【解决方案1】:

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

$result = json_encode($result, JSON_UNESCAPED_UNICODE);

docs

【讨论】:

  • 我使用 PHP 5.5,当我尝试这个时: $result = json_encode($result, JSON_UNESCAPED_UNICODE); $error = json_last_error_msg(); echo "[ERROR : $error]-----[$result]-----";我得到了同样的错误:[错误:格式错误的 UTF-8 字符,可能编码不正确]-----[]-----
【解决方案2】:

我正在运行 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 等效命名实体 (htmlentities) 的字符。对于此用例,请改用 mb_encode_numericentity

注意 3:iconvmb_encode_numericentity 都允许您指定字符串的编码。此外,它们也不适用于数组,因此您也需要为它们编写递归辅助函数。

【讨论】:

    猜你喜欢
    • 2015-02-05
    • 1970-01-01
    • 1970-01-01
    • 2013-11-10
    • 2014-11-25
    • 2017-09-09
    • 2022-01-14
    • 2016-09-09
    • 1970-01-01
    相关资源
    最近更新 更多