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

标签 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 
                    ) 
            )      
      )

我试试这个(带和不带 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)));

我收到:

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]-----[""]-----

当我尝试这个时:

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

我收到:

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]-----

我真的迷路了!

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

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

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

最佳答案

我正在运行 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将允许您指定字符串的编码。此外,它们也不适用于数组,因此您也需要为它们编写递归辅助函数。

关于php - 如何在 PHP 中将此数组转换为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20255954/

相关文章:

PHPExcel:自动下载并打开 Excel 文件

php - php中保护 session 劫持的有效方法

mysql - 使用 "|"作为分隔符将列转换为行

MySQL ON DELETE CASCADE 设置但不从其他表中删除行

java - 我如何使用 java 在 mysql 数据库中根据日期差异(来自另一个表的日期和当前日期)将表中的特定标志更新为 'Y'

c++ - 在函数内初始化 std::array

php - 抓取包含在 PHP 生成的元素中的文本

php - mysql 选择、计数、左连接和显示

arrays - Swift - 如何更新多维目录中的对象

javaScript 过滤嵌套对象和数组