Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json_encode on a two-dimensional array

Tags:

json

arrays

php

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?

like image 582
SUN Jiangong Avatar asked Dec 20 '25 08:12

SUN Jiangong


1 Answers

$arr = array();
while($row = mysql_fetch_assoc($result)) {
    $arr[] = $row; 
}
echo json_encode($arr);
like image 123
viam0Zah Avatar answered Dec 21 '25 22:12

viam0Zah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!