title: Build insert query from array
body: <p>I'm writing a small web service in PHP and I'm having a bit of trouble getting my head around the following scenario.</p>
  <p>My plan is to be able to send an array of data to a function, which would then build a query for MySQL to run. In the array I plan for the key to be the column name, and the value to be the value going in to the columns. i.e.
  $myData = array('userName'=&gt;'foo','passWord'=&gt;'bar');
  $myClass = new users();</p>
  <pre><code>$myClass-&gt;addUser($myData);
  </code></pre>
  <p>Then, in my function I currently have:</p>
  <pre><code>function addUser($usrData){
     foreach($usrData as $col =&gt; $val){
  
        // This is where I'm stuck..
  
     }
  }
  </code></pre>
  <p>Basically, I am wondering how I can separate the keys and values so that my query would become:</p>
  <pre><code>INSERT INTO `myTable` (`userName`,`passWord`) VALUES ('foo','bar');
  </code></pre>
  <p>I know I could probably do something like:</p>
  <pre><code>function addUser($usrData){
     foreach($usrData as $col =&gt; $val){
        $cols .= &quot;,&quot; . $col;
        $values .= &quot;,'&quot;.$val.&quot;'&quot;;
     }
  }
  </code></pre>
  <p>But I thought there might be a more elegant solution.</p>
  <p>This is probably something really simple, but I have never came across this before, so I would be grateful for any help and direction.</p>