title: foreach loop over multidimensional associative array
body: <p>I have an array ($this->taxBand) which has 3 key => value pairs.  Each value is another array:</p>
  
  <pre><code>array(3) {
    ["basic"]=&gt;
    array(3) {
      ["rate"]=&gt;
      int(20)
      ["start"]=&gt;
      int(0)
      ["end"]=&gt;
      int(31865)
    }
    ["higher"]=&gt;
    array(3) {
      ["rate"]=&gt;
      int(40)
      ["start"]=&gt;
      int(31866)
      ["end"]=&gt;
      int(150000)
    }
    ["additional"]=&gt;
    array(3) {
      ["rate"]=&gt;
      int(45)
      ["start"]=&gt;
      int(150001)
      ["end"]=&gt;
      NULL
    }
  }
  </code></pre>
  
  <p>I need to loop through not only the keys "basic", "higher" and "additional" but the arrays within them too.</p>
  
  <p>I'll be comparing the "start" and "end" values with another variable and applying a calculation based on the "rate".</p>
  
  <p>I've tried nested foreach in a couple of different ways using many examples I've found here and the official documentation and can only ever get it to return the "basic" array elements.</p>
  
  <p>Example:</p>
  
  <pre><code>foreach ($this-&gt;taxBand as $key =&gt; $band) {
  
              foreach ($band as $subKey =&gt; $value) {
  
                      // Do my stuff
  
              }
  
          }
  </code></pre>
  
  <p>If I return $band, I get:</p>
  
  <pre><code>array(5) {
    ["rate"]=&gt;
    int(20)
    ["start"]=&gt;
    int(0)
    ["end"]=&gt;
    int(31865)
  }
  </code></pre>
  
  <p>And $key returns:</p>
  
  <pre><code>string(5) "basic"
  </code></pre>
  
  <p>I'm clearing missing something quite basic and not fully understanding how to loop through these arrays properly and get all the data I need.</p>
  
  <p>Any help would be much appreciated. :)</p>
  
  <p>EDIT: Trying to show an example of how I plan to use this loop. It's difficult because of the other functions/variables:</p>
  
  <pre><code>foreach ($this-&gt;taxBand as $key =&gt; $band) {
  
                      if ($band["end"] !== null || $band["end"] &gt; 0) {
                          $band["amount"] = $this-&gt;get_lower_figure($this-&gt;totalTaxableAmount, $band["end"]) - $bandDeductions;
                      } else {
                          $band["amount"] = $this-&gt;totalTaxableAmount - $bandDeductions;
                      }
  
                  $band["percentage_amount"] = ($band["amount"] / 100) * $band["rate"];
                  $totalDeduction += $band["percentage_amount"];
                  $bandDeductions += $band["amount"];
  
                  return $totalDeduction;
  
          }
  </code></pre>
  
  <p>Assuming $this->totalTaxableAmount is 40000 the "percentage_amount" should return a float of 6373, which it does, for the basic band.  But it should also return 3254 from the higher band.</p>
  
  <p>get_lower_figure() just takes two arguments and checks which is less than the other.</p>