在PHP中嵌套JSON

在PHP中嵌套JSON,php,json,Php,Json,我有这个php $comment = array; while($row = mysql_fetch_array($sqlExec, MYSQL_ASSOC)){ $comment[$row['name']] = $row['comment']; } echo json_encode($comment); Having these results {"John":"Yo","Dan":"Hello","May":"Bye"} 问题是我实际上对John有两条评论(Zup,Yo),但正如您

我有这个php

$comment = array;
while($row = mysql_fetch_array($sqlExec, MYSQL_ASSOC)){
   $comment[$row['name']] = $row['comment'];
}
echo json_encode($comment);

Having these results
{"John":"Yo","Dan":"Hello","May":"Bye"}
问题是我实际上对John有两条评论(Zup,Yo),但正如您所看到的,它只显示John的最后一条评论,即“Yo”。所以我希望约翰的结果是

{"John":["Yo","Sup"]}
^这可能吗

我该怎么做?很抱歉,处理JSON仍然很困难。谢谢

这实际上是我的全部代码

while($row = mysql_fetch_array($sqlExec, MYSQL_ASSOC)){
    $comment[$row['name']] = $row['comment'];

    $sql_dup = "SELECT name, COUNT(name) AS dup_count 
                        FROM comment
                        GROUP BY name
                        HAVING (COUNT(name) > 1) 
                ";
    $sqlExec_dup = mysql_query($sql_dup, $connection);
    $row_dup = mysql_fetch_array($sqlExec_dup, MYSQL_ASSOC);
    if($row['name'] = $row_dup['name']){

        $sql_dup2 = "SELECT * FROM comment WHERE name = '{$row['name']}'";
        $sqlExec_dup2 = mysql_query($sql_dup2, $connection);
        while($row_dup2 = mysql_fetch_array($sqlExec_dup2, MYSQL_ASSOC)){
            $x += 1;
            if($x <= $row_dup['dup_count']){
                $comment[$row['name']][] = $row_dup2['comment'];
            }
        }
    }
}
while($row=mysql\u fetch\u数组($sqlExec,mysql\u ASSOC)){
$comment[$row['name']=$row['comment'];
$sql\u dup=“选择名称,计数(名称)作为dup\u计数
评论
按名称分组
具有(计数(名称)>1)
";
$sqlExec\u dup=mysql\u查询($sql\u dup,$connection);
$row\u dup=mysql\u fetch\u数组($sqlExec\u dup,mysql\u ASSOC);
如果($row['name']=$row_dup['name'])){
$sql_dup2=“从注释中选择*,其中name='{$row['name']}';
$sqlExec\u dup2=mysql\u查询($sql\u dup2,$connection);
而($row\u dup2=mysql\u fetch\u数组($sqlExec\u dup2,mysql\u ASSOC)){
$x+=1;

如果($x,则必须检查它是否已经存在,如果已经存在,则创建一个数组(或者从一开始就这样做)


我要指出的是,第一种解决方案将是非常可取的,因为它更具后续性。

您必须检查它是否已经存在,如果已经存在,请创建一个数组(或者从一开始就这样做)


我应该指出,第一种解决方案更可取,因为它更具后果性。

是的,它是可能的。而不是这一行:

$comment[$row['name']] = $row['comment'];
使用以下命令:

$comment[$row['name']] = array('Yo', 'Sup');

并将数组的内容替换为您希望从数据库获得的问候语。

是的,这是可能的。而不是这一行:

$comment[$row['name']] = $row['comment'];
使用以下命令:

$comment[$row['name']] = array('Yo', 'Sup');
并将数组的内容替换为希望从数据库获得的问候语。

简单更改:

$comment[$row['name']] = $row['comment'];

每次出现相同名称时,$comment数组的每个name元素都会被覆盖。

简单更改:

$comment[$row['name']] = $row['comment'];


$comment数组的每个name元素在每次出现相同的名称时都会被覆盖。

是的,这是可能的,但您需要对此进行一些预处理:

$comment = array;
while($row = mysql_fetch_array($sqlExec, MYSQL_ASSOC)){
   if(!isset($comment[$row['name']])) {
     $comment[$row['name']] = array();
   }
   $comment[$row['name']][] = $row['comment'];
}
echo json_encode($comment);

是的,这是可能的,但您需要为此进行一些预处理:

$comment = array;
while($row = mysql_fetch_array($sqlExec, MYSQL_ASSOC)){
   if(!isset($comment[$row['name']])) {
     $comment[$row['name']] = array();
   }
   $comment[$row['name']][] = $row['comment'];
}
echo json_encode($comment);