title: Losing data in foreach loop
body: <p>Struggling with this project - I have a multidimensional associative array that is being parsed to pull out tablenames, fieldnames and data for insertion into a mysql db.  In the array2query function, if the key:value is a top level pair, then the $tablename is set to 'project', otherwise $tablename=array name (discarding array[0] array names).  Data is NOT coming users, it's coming from API call in a json file.  Implementation will use PDO for writing to mysql db.</p>
  
  <p>I'm getting a undeclared variable notice on $sql.  If I declare it inside the function, it gets reset to empty during the foreach loop and I lose part of the $sql string that has been built (all 'project' tablename data).</p>
  
  <p>I also need to discard data where $element is null or empty but not 0 or 'false' which I can't seem to find a way to do cleanly.</p>
  
  <p>How do I properly handle the NOTICE without losing the existing string? </p>
  
  <pre><code>&lt;?php
  
  error_reporting(E_ALL);
  
  //read the json file contents
  $data = file_get_contents('c:\wamp64\www\json\test.json');
  $data1 = preg_replace('/("\w+"):(-?\d+(\.\d+)?)/', '\\1:"\\2"', $data);
  $array1 = json_decode($data1, true);
  
  function array2query(array $array1, $tableName = ''){
         $sql="";  // not declaring variable results in NOTICE; declaring results in $sql being reset to "" during foreach loop
         foreach ($array1 as $key =&gt; $element) {
              if (is_array($element)) {
              // if key is integer then skip - it is the first level of array, keep the $tablename    
                  $key = is_int($key) ? $tableName : $key;
                  array2query($element, $key);
              } else {
                  if ($tableName === '') {
                  $tableName = 'project';
                  }
              $sql .= 'INSERT INTO ' . $tableName . ' (' . rtrim($key, ',') . ') VALUES (' . rtrim($element, ',') . ');';
              //    }        
              } 
         }    
         echo $sql;  //dump to screen; missing non-nested values from tablename 'project' 
         return $sql;
      }
  
  $array_query=array2query($array1);
  </code></pre>
  
  <p>Thank you for looking at this.  Test data is at <a href="http://pastebin.com/iSvqCMxb" rel="nofollow">http://pastebin.com/iSvqCMxb</a></p>
  
  <p>EDIT:
  Screen dump from echo:</p>
  
  <pre><code>INSERT INTO Contacts (ContactType) VALUES (Sales);
  INSERT INTO Contacts (ContactAddressId) VALUES (ec4539c0-9012-4b4f-a590-bee11cc91109);
  INSERT INTO Contacts (ExportId) VALUES ();
  INSERT INTO Vendors (VendorId) VALUES (c3abfde4-4390-4e09-aa5f-60284613c8c5);
  INSERT INTO Vendors (Name) VALUES (Fabrication Specialists);
  INSERT INTO Vendors (ShortName) VALUES (Fabspec);
  </code></pre>
  
  <p>This is missing all "INSERT INTO (project)... " statements.</p>