2

I have a problem using JSON and arrays.

Here is my code:

while($row = mysql_fetch_assoc($result)){ echo json_encode($row); }

The result is:

{"id":"1","title":"event1","start":"2009-11-10 14:18:15","end":"2009-11-03 14:38:22","allDay":"false","url":null}{"id":"2","title":"event2","start":"2009-11-09 15:41:20","end":"2009-11-10 16:41:25","allDay":"false","url":null}

But I want the result to look like this:

[{"id":"1","title":"event1","start":"2009-11-10 14:18:15","end":"2009-11-03 14:38:22","allDay":"false","url":null},{"id":"2","title":"event2","start":"2009-11-09 15:41:20","end":"2009-11-10 16:41:25","allDay":"false","url":null}]

How can I accomplish this?

2 Answers 2

8
$arr = array();
while($row = mysql_fetch_assoc($result)) {
    $arr[] = $row; 
}
echo json_encode($arr);
Sign up to request clarification or add additional context in comments.

Comments

0
$myjsons = array();
while($row = mysql_fetch_assoc($result)){ 
    $myjsons[] = json_encode(array($row)); 
}
print_r($myjsons);

Comments

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.