dazzathedrummer Posted May 4, 2011 Share Posted May 4, 2011 Hi, I have the following code that takes a list from a mySQL db and puts it into an array that shows 'gig date, venue, city' per entry: - while ($row = mysql_fetch_array($result,MYSQL_ASSOC)) { $array[] = ($row['date'].', '.$row['gl_venue'].', '.$row['gl_city']); } $output = json_encode($array); this outputs JSON like so: ["06-05-2011, O'neills, Southend","07-05-2011, Power League, Peterborough","14-05-2011, Queen Victoria hall, Oundle",.......] how do I add keys into the array so I get something like: {"gigs":[ { "gig": "date, venue, city",} ]} at least I think so anyway!?! What I want to do is eventually be able to parse a JSON listing into Objective-C and be able to call key/pairs into various different parts of an app. I'm pretty new to this so any advice would be great. Quote Link to comment https://forums.phpfreaks.com/topic/235492-array-from-mysql-into-json-how-do-i-add-keys/ Share on other sites More sharing options...
trq Posted May 4, 2011 Share Posted May 4, 2011 $array = array(); $array['gigs'] = array(); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $array['gigs'][] = $row; } $output = json_encode($array); Should get you.... { "gigs":[ {"date": "06-05-2011", "gl_venue": "O'neills", "gl_city": "Southend"}, {"date": "07-05-2011", "gl_venue": "Power League", "gl_city": "Peterborough"} ] } And so on. Quote Link to comment https://forums.phpfreaks.com/topic/235492-array-from-mysql-into-json-how-do-i-add-keys/#findComment-1210311 Share on other sites More sharing options...
dazzathedrummer Posted May 4, 2011 Author Share Posted May 4, 2011 excellent, that works brilliantly!! now I just need to figure out how to get that into an NSDictionary - but that's a question for a different forum haha! thanks again! ....just out of interest, why is $array declared twice? Darren Quote Link to comment https://forums.phpfreaks.com/topic/235492-array-from-mysql-into-json-how-do-i-add-keys/#findComment-1210321 Share on other sites More sharing options...
trq Posted May 4, 2011 Share Posted May 4, 2011 ....just out of interest, why is $array declared twice? Because you have an array within an array. It's not really necessary really though. Quote Link to comment https://forums.phpfreaks.com/topic/235492-array-from-mysql-into-json-how-do-i-add-keys/#findComment-1210323 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.