Howey-Ya-Doing
Howey-Ya-Doing

Reputation: 65

JSON Encode Help

Currently, I have a JSON encoded array that looks like this:

[
{
    "username1": {
        "post": {
            "some_key" : "some_value"
        }
    }
},
{
    "username1": {
        "post": {
            "some_key" : "some_value"
        }
    }
}
]

But how can I make it so that the json follows this pattern:

username -> array_of_posts -> post -> values

instead of the current pattern?

Thanks

Here is my current code:

    while ($row = mysql_fetch_assoc($query)) {
            $row['username'] = $username;
            $returns[][$username]["post"] = $row;
        }
    }   
    echo json_encode(array_values($returns));

Upvotes: 1

Views: 376

Answers (1)

Jonas Schmid
Jonas Schmid

Reputation: 5501

$returns[$username][] = $row;

echo json_encode($returns);

Upvotes: 1

Related Questions