Json_encode on a two dimensional array

I have a problem using JSON and arrays.

Here is my code:

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

Result:

{"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 do this?

+3
source share
2 answers
$arr = array();
while($row = mysql_fetch_assoc($result)) {
    $arr[] = $row; 
}
echo json_encode($arr);
+8
source
$myjsons = array();
while($row = mysql_fetch_assoc($result)){ 
    $myjsons[] = json_encode(array($row)); 
}
print_r($myjsons);
0
source

Source: https://habr.com/ru/post/1721791/


All Articles