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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.