title: PHP foreach loop using one entry twice
body: <p>I'm just experimenting a little with PHP and PDO working with a MySQL database and I'm a little stumped as to why after getting the results, storing them correctly in a multi-dimensional array and looping through them it outputs one of the array data twice.</p>
  
  <p>Essentially here's the query to grab the data:</p>
  
  <pre><code>SELECT b.Price, b.ImgURL, m.Name, f.ID, f.Family, f.URL FROM Products AS b INNER JOIN Manufacturers AS m ON m.ID = b.Manufacturer INNER JOIN FamilyLookUp AS l ON l.Product = b.ID INNER JOIN Families AS f ON f.ID = l.Family GROUP BY f.ID ORDER BY b.Price ASC
  </code></pre>
  
  <p>I was hoping with this to get 1 row returned for each Family, which it works correctly both in the PHPMyAdmin query and also when print_r() the results.</p>
  
  <p>I then store in:</p>
  
  <pre><code>$families[] = array('ID' =&gt; $f['ID'], 'Manufacturer' =&gt; $f['Name'], 'Family' =&gt; $f['Family'], 'URL' =&gt; $f['URL'], 'IMG' =&gt; $f['ImgURL'], 'Price' =&gt; $f['Price'], 'ScentCount' =&gt; 0);
  </code></pre>
  
  <p>Which also works correctly when doing a print_r() and when just looping through with a foreach loop echoing out the ID for each entry it returns 1234567 (all 7 Family IDs)</p>
  
  <p>Then I run another query:</p>
  
  <pre><code>try{
  $sqlCmd = "SELECT COUNT(*) FROM FamilyLookUp WHERE Family=:fID";
  $s = $pdo-&gt;prepare($sqlCmd);
  foreach($families as &amp;$fam){
  $s-&gt;bindValue(':fID', $fam['ID']);
  $s-&gt;execute();
  $fam['ScentCount'] = $s-&gt;fetchColumn();
  }
  }
  </code></pre>
  
  <p>This also gets the correct counts and properly stores them in the array for the number of items within each family. So all good up to now.</p>
  
  <p>The problem occurs when I:</p>
  
  <pre><code>foreach($families as $fam):
          ?&gt;
  
          &lt;div class="product-listing"&gt;
          &lt;?php echo $fam['ID']; ?&gt;
              &lt;div class="product-listing-image"&gt;
                  &lt;a href="&lt;?php echo $fam['URL']; ?&gt;"&gt;&lt;img alt="" src="&lt;?php echo $fam['IMG']; ?&gt;"&gt;&lt;/a&gt;
              &lt;/div&gt;
              &lt;div class="product-listing-details"&gt;
  
                  &lt;a href="&lt;?php echo $fam['URL']; ?&gt;"&gt;&lt;h3&gt;&lt;?php echo strtoupper($fam['Manufacturer']); if($fam['Family'] != ""){ echo strtoupper(' - ' . $fam['Family']);} ?&gt;&lt;/h3&gt;&lt;/a&gt;
                  &lt;?php if($fam['ScentCount'] == 1): ?&gt;
                  &lt;span class="product-scent-count"&gt;&lt;?php echo $fam['ScentCount']; ?&gt; Scent&lt;/span&gt;
                  &lt;span class="product-price-value"&gt;£&lt;?php echo $fam['Price']/100; ?&gt;&lt;/span&gt;
                  &lt;?php elseif($fam['ScentCount']&gt;1): ?&gt;
                  &lt;span class="product-scent-count"&gt;&lt;?php echo $fam['ScentCount']; ?&gt; Scents&lt;/span&gt;
                  &lt;span class="product-price-value"&gt;From £&lt;?php echo $fam['Price']/100; ?&gt;&lt;/span&gt;
                  &lt;?php endif;?&gt;
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;?php
              endforeach;
          ?&gt;
  </code></pre>
  
  <p>After doing this, it outputs correctly for the first 6 families of data, but for some reason it outputs a duplicate of the 6th instead of the actual 7th. When doing a print_r of all the data in the line before the start of the foreach loop, it returns all the correct data and yet within the foreach loop there becomes 1 duplicate array in the place of the 7th originally correct array.</p>
  
  <p>Any advice would be awesome. </p>
  
  <p>Edit for Kohloth's answer(the print_r followed directly by foreach vardump):</p>
  
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [ID] =&gt; 1
          )
  
      [1] =&gt; Array
          (
              [ID] =&gt; 7
          )
  
      [2] =&gt; Array
          (
              [ID] =&gt; 2
          )
  
      [3] =&gt; Array
          (
              [ID] =&gt; 3
          )
  
      [4] =&gt; Array
          (
              [ID] =&gt; 4
          )
  
      [5] =&gt; Array
          (
              [ID] =&gt; 6
          )
  
      [6] =&gt; Array
          (
              [ID] =&gt; 5
          )
  
  )
              array(7) {
    ["ID"]=&gt;
    string(1) "1"
  }
  
      array(7) {
    ["ID"]=&gt;
    string(1) "7"
  }
  
      array(7) {
    ["ID"]=&gt;
    string(1) "2"
  }
  
      array(7) {
    ["ID"]=&gt;
    string(1) "3"
  }
  
      array(7) {
    ["ID"]=&gt;
    string(1) "4"
  }
  
      array(7) {
    ["ID"]=&gt;
    string(1) "6"
  }
  
      array(7) {
    ["ID"]=&gt;
    string(1) "6"
  }
  </code></pre>