Jump to content

Array from mySQL into JSON - how do I add keys?


Recommended Posts

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.

 

$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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.