title: Convert JSON array into an insert query string in php
body: <p>i have a JSON array and i need to convert it into insert query string,
  does anyone have idea how to do that?</p>
  
  <p>here's the JSON string</p>
  
  <pre><code>$array = '[
     {
       "table":"customer",
       "data":
         [
           {
             "name":"Johny",
             "address":"jl. abc no 5",
             "where" :
  
               {
                 "customerid":1
               }
  
           },
           {
             "name":"Doko",
             "address":"jl. kerinci 7"
             "where" :
  
               {
                 "customerid":2
               }
           }
         ]
     },
     {
       "table":"supplier",
       "data":
         [
           {
             "id":123
           },
           {
             "id":234
           }
         ]
     }
  ]
  ';
  </code></pre>
  
  <p>then i use json_decode to convert it into an array</p>
  
  <pre><code>$obj = json_decode($array,true);
  </code></pre>
  
  <p>here's the workaround i've done so far.</p>
  
  <pre><code>foreach($obj as $result){
    $table = $result['table'] . "&lt;/br&gt;";
    $data =  $result['data'];
    $string = "INSERT INTO $table (";
    foreach($data as $subdata ){
      foreach($subdata as $key=&gt;$val){
        $string .= "$key,";
        $string .= ")VALUES (";
        $string .= "'$val'" . ",";
      }
      $string .= ");";
    }
    echo $string."&lt;/br&gt;";
  }
  </code></pre>
  
  <p>but it doesn't work like i want to,</p>
  
  <p>what i need is to convert it into query like</p>
  
  <pre><code>INSERT INTO customer(customerid,address)values('1','jl.abc no 5');
  INSERT INTO customer(customerid,address)values('2','jl. kerinci no 7');
  INSERT INTO supplier(id)values('123');
  INSERT INTO supplier(id)values('234');
  </code></pre>
  
  <p>thanks in advance.</p>
  
  <p>PS: the array could be more than two arrays or even just one.</p>
  
  <p>===========================================================================</p>
  
  <p>updates : read array then split it into update query string 
  like</p>
  
  <pre><code>UPDATE customer SET name="JOHNY", address="jl. abc no 5";
  UPDATE customer SET name="Doko", address="jl. kerinci 7";
  </code></pre>
  
  <p>when "where" key exists and ignore it when "where" key doesn't exist</p>