Barry
Barry

Reputation: 1587

Nesting JSON in PHP

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. Thanks

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.

Upvotes: 0

Views: 187

Answers (4)

Mchl
Mchl

Reputation: 62395

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

Upvotes: 1

Tim Dorr
Tim Dorr

Reputation: 4931

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.

Upvotes: 0

Jonah
Jonah

Reputation: 10091

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.

Upvotes: 0

Tom van der Woerdt
Tom van der Woerdt

Reputation: 29985

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.

Upvotes: 2

Related Questions