0

I am trying to display all the users in my database in JSON form in an array. This is what I have - but it is not in the form that I want it in

{
  "username": [
    "bobgreen",
    "jacksmitd",
    "samson",
  ]
  "forename": [
    "Bob",
    "Jack",
    "Sam",
  ],
  "surname": [
    "O'Conor",
    "Smitd",
    "Son",
  ]
}

This is my code that gives this output...

if(mysqli_num_rows($outcome)){
    while($row = mysqli_fetch_assoc($outcome)){
        $finalOutcome["username"][] = $row["username"];
        $finalOutcome["forename"][] = $row["forename"];
        $finalOutcome["surname"][] = $row["surname"];
    }
    echo json_encode($finalOutcome); 
}

I want to store all of the relevant details within braces so like:

{
    "username":"bobgreen"
    "forename":"Bob"
    "surname":"O'Conor"
}
{
    "username":"jacksmitd"
    "forename":"Jack"
    "surname":"Smitd"
}
{
    "username":"samson"
    "forename":"Sam"
    "surname":"Son"
}

Also how do I extract all the relevant data from each brace and store in a list in Java? Because I am trying to store all the details on a per users basis and I wasn't sure how to do it with my first JSON array

1
  • Sorry, Java. I want to use the username, forename and surname for every user as I want to add it to a list. Commented Feb 18, 2016 at 1:29

2 Answers 2

1

Regarding main question:

You have to use this syntax:

while( $row = mysqli_fetch_assoc( $outcome ) )
{
    $finalOutcome[] = array
    (
        "username" => $row["username"],
        "forename" => $row["forename"],
        "surname"  => $row["surname"]
    );
}
echo json_encode( $finalOutcome );

Substantially, in your original query you increment $finalOutcome sub-arrays instead of $finalOutcome

Sign up to request clarification or add additional context in comments.

12 Comments

My answer is almost the same. The output will be the same with a different solution that also works.
It doesn't work! I'm getting Parse error: syntax error, unexpected ';', expecting ')' in PostMan
@AndreCardoso No. If the id start from 1, i.e., you will obtain {"1":{"username":"johnd","forename":"john","surname":"doe"}} instead of [{"username":"johnd","forename":"john","surname":"doe"}]
@SamuelGeorgeszusz yes, you are right, typo error inside definition! replace ; with ,
Thank you. It works. Now the question is how do I extract out the information for each username? I want to do add them to a list in Java
|
0

You can to do a better solution (comparated to the question code):

if(mysqli_num_rows($outcome)){
while($row = mysqli_fetch_assoc($outcome)){

    $userId = $row["id"];
    $finalOutcome[$userId]["username"] = $row["username"];
    $finalOutcome[$userId]["forename"] = $row["forename"];
    $finalOutcome[$userId]["surname"] = $row["surname"];
}
echo json_encode($finalOutcome); 

And the output will be:

[
    {
        "username":"username0",
        "forename":"forename0",
        "surname":"surname0"
    }, 
    {
        "username":"username1",
        "forename":"forename1",
        "surname":"surname1"
    }
]

4 Comments

No, if $row["id"] doesn't start from 0 and/or have a gap
Ok, but what is the problem if doesn't start with 0? The output will be exactly the same! This solution is only to group the info by user, their ids isn't used as you can see.
if the id doesn't start from 0, json convert it in object instead of array. json doesn't allow associative arrays
Don't be angry, Andre. Everyone makes mistakes (see this my answer, i.e.). It is not a race, and I don't think that my answer is better than yours. On the contrary, your answer could be better than mine, because it returns also the id. I have only remarked than with your answer OP obtains an object instead of an array. I remarked this because I'm better than you? Not at all, only because I stumbled in this error before you. I'm sure that you have a lot of stuff to teach me and that your contribute in SO is valuable. Peace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.