title: Iterating through two POST arrays of different length to find customers who bought x,y,z but didn't buy x,y,x,z
body: <p>All the answers out there deal with iterating two or more arrays of the same length, or with the same keys, but not here. I tried all of the ideas out there to no avail.</p>
  
  <p>I have two multiple selects submitted in the same POST form - products bought and products not bought. They contain the same products, but as multi selects will rarely have produce the same items in array (think of how many combinations there are.) The first <code>foreach</code> works fine and pulls from MySQL all customers who had any of the selected "bought" items. However the second nested <code>foreach</code> doesn't iterate - it just takes the first value from the "hasn't brought" array.</p>
  
  <p>HTML part:</p>
  
  <pre><code>Find customers who have bought:
  
  &lt;form method="post" action=""&gt;
  &lt;select name="bought[]" size="5" multiple="multiple"&gt;
  &lt;option value="RD"&gt; Brown T Shirt &lt;/option&gt;
  &lt;option value="RDX"&gt; Red T Shirt &lt;/option&gt;
  &lt;option value="HD"&gt; White T Shirt &lt;/option&gt;
  &lt;option value="CB"&gt; Black T Shirt&lt;/option&gt;
  &lt;option value="DA"&gt; Green T Shirt &lt;/option&gt;
  &lt;/select&gt;
  
  But who haven't bought:
  
  &lt;select name="notbought[]" size="5" multiple="multiple"&gt;
  &lt;option value="RD"&gt; Brown T Shirt &lt;/option&gt;
  &lt;option value="RDX"&gt; Red T Shirt &lt;/option&gt;
  &lt;option value="HD"&gt; White T Shirt &lt;/option&gt;
  &lt;option value="CB"&gt; Black T Shirt&lt;/option&gt;
  &lt;option value="DA"&gt; Green T Shirt &lt;/option&gt;
  &lt;/select&gt;
  
  &lt;div class="buttons&gt;
  &lt;button class="large" type="submit"&gt;Get the CSV!&lt;/button&gt;
  &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Then the PHP:</p>
  
  <pre><code>&lt;?php
  foreach($_POST['bought'] as $bought) {
  foreach($_POST['notbought'] as $notbought){
  
  $selectsubs = mysqli_query("SELECT First_Name, Last_Name, Email, Territory
                  FROM Customers
                  WHERE Customer_Type = 'Active'
                  AND Products_Bought LIKE '%$bought%'
                  AND Products_Bought NOT LIKE '%$notbought%'
                  ");
  
  
   while($row = mysqli_fetch_assoc($selectsubs))  {
  
  $csvWriter-&gt;addRow();
  $csvWriter-&gt;addCol($row['Email']);
  $csvWriter-&gt;addCol($row['First_Name']);
  $csvWriter-&gt;addCol($row['Last_Name']);
  $csvWriter-&gt;addCol($row['Territory']);
      }
    }
  }
  $csvWriter-&gt;writeFile("customers.csv");
  ?&gt;
  </code></pre>