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 Quote 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; Quote Link to comment https://forums.phpfreaks.com/topic/244114-arrays-help-needed/#findComment-1253666 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.