Jump to content

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

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.