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?

TRiG
10.7k9 gold badges62 silver badges113 bronze badges
asked Nov 3, 2009 at 15:10

2 Answers 2

8
$arr = array();
while($row = mysql_fetch_assoc($result)) {
 $arr[] = $row; 
}
echo json_encode($arr);
answered Nov 3, 2009 at 15:19
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);
answered Nov 3, 2009 at 15:16

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.