开发者

Nesting JSON in PHP

开发者 https://www.devze.com 2023-02-08 09:40 出处:网络
I have this in php $comment = array; while($row = mysql_fetch_array($sqlExec, MYSQL_ASSOC)){ $comment[$row[\'name\']] = $row[\'comment\'];

I have this in 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"}

The problem is I actually have two comments(Zup,Yo) for John, but as you can see, it only displays the last comment of John which is "Yo". So I wanted the results of John to be

{"John":["Yo","Sup"]}

^ is this possible?

How can I do that? Im sorry still having a hard time dealing with JSON. Than开发者_开发技巧ks

This is actually my full code

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

If the name has a duplicate, meaning it has more than one comment, still cant get my desired results.


You would have to check whether it already exists or not, and if it does, create an array (or do that from the start)

// Create arrays with names
$comment[$row['name']][] = $row['comment'];

or

// Check if there's an array
if (isset($comment[$row['name']])) {
    if (is_array($comment[$row['name']])) {
        $comment[$row['name']][] = $row['comment'];
    } else {
        $comment[$row['name']] = array($comment[$row['name']], $row['comment']);
    }
} else {
    $comment[$row['name']] = $row['comment'];
}

I should point out that the first solution would be very much preferred because it is a lot more consequent.


Yes, it is possible, but you need to do some preprocessing for that:

$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);


Yes, it's possible. Instead of this line:

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

Use this:

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

And replace the contents of the array with the greetings you want from the database.


Simple change:

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

to

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

Each name element of the $comment array was being overwritten each time the same name came up.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号