doforumda Posted August 7, 2011 Share Posted August 7, 2011 Hi. I want to use json with php. I am getting data from database. now I want to format that in this way { msg: [{"id":"1"},{"id":"2"},{"id":"3"},{"id":"4"},{"id":"5"}]} to work with it on client side using json. here is my php code include("db.php"); $query = mysql_query("SELECT * FROM data_table"); $id = ""; while($row = mysql_fetch_assoc($query)){ $id = $row['id']; $JSON_array['msg'] = array(); $JSON_array['msg']['id'] = $id; $JSON_response = json_encode($JSON_array); header("Contents: type/json"); echo $JSON_response; } help please Link to comment https://forums.phpfreaks.com/topic/244114-arrays-help-needed/ Share on other sites More sharing options...
trq Posted August 7, 2011 Share Posted August 7, 2011 include("db.php"); $query = mysql_query("SELECT * FROM data_table"); $id = ""; $result = array(); while($row = mysql_fetch_assoc($query)){ $result[]['id'] = $row['id']; } $JSON_response = json_encode($result); header("Contents: type/json"); echo $JSON_response; Link to comment https://forums.phpfreaks.com/topic/244114-arrays-help-needed/#findComment-1253666 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.