title: Using nested loops to build array with unique values (PHP/XML)
body: <p>I'm using PHP to collect attributes from an XML file.</p>
  
  <p>Eg:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt; 
      &lt;menu restaurant="edible things"&gt; &lt;!-- Root Node --&gt;
          &lt;section category="Fruit"&gt;
              &lt;item&gt;  Apple
                  &lt;price variant="small"&gt; 2.50    &lt;/price&gt;    
                  &lt;price variant="large"&gt; 3.75    &lt;/price&gt;
              &lt;/item&gt;
              &lt;item&gt;  Banana  
                  &lt;price variant="small"&gt; 1.85    &lt;/price&gt;    
                  &lt;price variant="medium"&gt;    2.00    &lt;/price&gt;    
                  &lt;price variant="large"&gt; 2.85    &lt;/price&gt;
              &lt;/item&gt;
          &lt;/section&gt;
          &lt;section category="Instruments"&gt;            
              &lt;item&gt;  Piano   
                  &lt;price variant="red"&gt;   100.00  &lt;/price&gt;    
                  &lt;price variant="yellow"&gt;    3000.00 &lt;/price&gt;
              &lt;/item&gt;
              &lt;item&gt;  Tuba    
                  &lt;price variant="red"&gt;   60.00   &lt;/price&gt;    
                  &lt;price variant="yellow"&gt;    250.00  &lt;/price&gt;
              &lt;/item&gt;
          &lt;/section&gt;      
      &lt;/menu&gt;
  </code></pre>
  
  <p>My goal is to create an array/list of the price variants with no duplicates.  To that end I have implemented the following PHP (collected in a web page):</p>
  
  <pre><code>&lt;?php 
  foreach ($xml-&gt;section as $section) {
      echo '&lt;h2&gt;'.$section['category'].'&lt;/h2&gt;&lt;br /&gt;';
      $arr = array();
      foreach ($section-&gt;item as $item) {
          foreach ($item-&gt;price as $price) {
              if (!in_array($price['variant'], $arr)) {
                  array_push($arr, $price['variant']);
              }
              else {
              }
          }
      }
      $arrlength = count($arr);
      for($x = 0; $x &lt; $arrlength; $x++) {
          echo $arr[$x];    
          echo '&lt;br&gt;';
          }
  }
  ?&gt;
  </code></pre>
  
  <p>Unfortunately, in this instance I get an array that contains every attribute value, including duplications.  If I cut either foreach loop out, I get only (a) attributes for the first item in section, or (b) the first variant for all items, respectively.</p>
  
  <p>I realize that I'm having a problem with understanding how nesting works.  How do you build an array with only unique values?</p>