views:

369

answers:

2

Hello everyone,

I have a problem when i use json and array. And i need your help.

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 is 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? Thanks

A: 
$myjsons = array();
while($row = mysql_fetch_assoc($result)){ 
    $myjsons[] = json_encode(array($row)); 
}
print_r($myjsons);
inkedmn
Doesn't work also. The output is: Array
garcon1986
sorry, it's fixed.
inkedmn
@inkadmn Thanks all the same. cheers
garcon1986
+2  A: 
$arr = array();
while($row = mysql_fetch_assoc($result)) {
    $arr[] = $row; 
}
echo json_encode($arr);
Török Gábor
YES! It works!! Thanks a lot.
garcon1986