title: Parse JSON To Create SQL Insert Statements in PHP
body: <p>I'm a newbie programmer trying to find my way in the world. I've got my hands on JSON data that I'm trying to parse out into SQL statements to populate multiple database tables. I would like to loop through each dimension of the array and pull out specific parts of it to create an INSERT statement I can just pass to MySQL. I'm not sure if this is the best way to populate separate tables with data from one JSON file but it's the only thing I can think of. MySQL tables are separated so there is a table for a person, a table for address type, a table for address, a table for phone, a table for email etc. This is to account for a person record having numerous phone numbers, email addresses etc.</p>
  
  <p>I have been able to decode the JSON from an external URL. Here is the code and a sample of the output using print_r.</p>
  
  <pre><code>$json_string = 'http://....';
  
  $jsondata = file_get_contents($json_string);
  
  $data = json_decode($jsondata, TRUE);
  </code></pre>
  
  <p>1 Record Sample:</p>
  
  <pre><code>Array ( [objects] =&gt; Array ( [0] =&gt; Array ( [first_name] =&gt; Anthony [last_name] =&gt; Perruzza [name] =&gt; Anthony Perruzza [elected_office] =&gt; City councillor [url] =&gt; http://www.toronto.ca/councillors/perruzza1.htm [gender] =&gt; [extra] =&gt; Array ( ) [related] =&gt; Array ( [boundary_url] =&gt; /boundaries/toronto-wards/york-west-8/ [representative_set_url] =&gt; /representative-sets/toronto-city-council/ ) [source_url] =&gt; http://www.toronto.ca/councillors/perruzza1.htm [offices] =&gt; Array ( [0] =&gt; Array ( [tel] =&gt; 416-338-5335 ) ) [representative_set_name] =&gt; Toronto City Council [party_name] =&gt; [district_name] =&gt; York West (8) [email] =&gt; councillor_perruzza@toronto.ca [personal_url] =&gt; [photo_url] =&gt; ) ) [meta] =&gt; Array ( [next] =&gt; /representatives/?limit=1&amp;offset=1 [total_count] =&gt; 1059 [previous] =&gt; [limit] =&gt; 1 [offset] =&gt; 0 ) )
  </code></pre>
  
  <p>JSON Code Sample:</p>
  
  <pre><code>{"objects": [
      {"first_name": "Keith",
       "last_name": "Ashfield",
       "name": "Keith Ashfield",
       "elected_office": "MP",
       "url": "http://www.parl.gc.ca/MembersOfParliament/ProfileMP.aspx?Key=170143&amp;Language=E",
       "gender": "",
       "extra": {},
       "related": {
            "boundary_url": "/boundaries/federal-electoral-districts/13003/", 
            "representative_set_url": "/representative-sets/house-of-commons/"
       },
       "source_url": "http://www.parl.gc.ca/MembersOfParliament/MainMPsCompleteList.aspx?TimePeriod=Current&amp;Language=E",
       "offices": [
           { "type": "legislature",
             "fax": "613-996-9955",
             "postal": "House of Commons\nOttawa, Ontario\nK1A 0A6",
             "tel": "613-992-1067"
           },
           { "type": "constituency",
             "fax": "506-452-4076",
             "postal": "23 Alison Blvd (Main Office)\nFredericton, New Brunswick\nE3C 2N5",
             "tel": "506-452-4110"
           }
       ],
       "representative_set_name": "House of Commons",
       "party_name": "Conservative",
       "district_name": "Fredericton",
       "email": "keith.ashfield@parl.gc.ca",
       "personal_url": "",
       "photo_url": "http://www.parl.gc.ca/MembersOfParliament/Images/OfficialMPPhotos/41/AshfieldKeith_CPC.jpg"
      }
   ],
    "meta": {
         "next": "/representatives/house-of-commons/?limit=1&amp;offset=1",
         "total_count": 307,
         "previous": null,
         "limit": 1,
         "offset": 0
     }
   }
  </code></pre>
  
  <p>Any help you can offer would be greatly appreciated. I've been pulling my hair out for the last few days trying to figure it out.</p>
  
  <p>I've tried customizing code like the following to make it work but I haven't been able to hit the sweet spot. Please not, this code doesn't reference my data or variables. I deleted what didn't work for me. I'm just including it to give you an idea what I've tried.</p>
  
  <pre><code>foreach ($data as $item) {
      echo $item-&gt;{'first_name'} . "&lt;br/&gt;";
      echo $item-&gt;{'last_name'};
  }
  </code></pre>
  
  <p>If you could point me in the direction of being able to parse out data from any level of the array it would be greatly appreciated.</p>
  
  <p>Best,</p>
  
  <p>S</p>