title: Use php array to generate a single MySQL INSERT query
body: <p>I have a dynamically generated table listing all users in a given group, and each user has a checkbox next to their name.  The group admin may select/deselect these checkboxes to include/exclude a user.  In the MySQL table, there is a status column for each user that is either a <code>0</code> or a <code>1</code>.  I use hidden values to handle the unchecked status of boxes and POST the data as an array with each user's unique ID as the key.</p>
  
  <p>So my POST data is an array that looks thus:</p>
  
  <p><code>Array ( [105] =&gt; 1 [106] =&gt; 1 [107] =&gt; 0 [108] =&gt; 0 [109] =&gt; 1 )</code></p>
  
  <p>Now, I need to update the db.  Here is how I want the query to look:</p>
  
  <pre><code>INSERT INTO users (uid,status) VALUES (105,1),(106,1),(107,0),(108,0),(109,1)
  ON DUPLICATE KEY UPDATE status=VALUES(status);
  </code></pre>
  
  <p>Of course, that doesn't INSERT anything, but it updates every row with a single query.</p>
  
  <p>What I can't figure out is <strong>how to use the array to generate the query.  Especially given that the number of users (the number of items in the array) will vary.</strong></p>
  
  <hr>
  
  <p>After additional research, it seems that my question may boil down to:</p>
  
  <p>Is it possible to use <code>implode</code> to turn this array:</p>
  
  <p><code>Array ( [105] =&gt; 1 [106] =&gt; 1 [107] =&gt; 0 [108] =&gt; 0 [109] =&gt; 1 )</code></p>
  
  <p>into this string:</p>
  
  <p><code>(105,1),(106,1),(107,0),(108,0),(109,1)</code></p>
  
  <p>with the flexibility to have more or fewer items in the array?</p>