2

I have an array ($this->taxBand) which has 3 key => value pairs. Each value is another array:

array(3) {
  ["basic"]=>
  array(3) {
    ["rate"]=>
    int(20)
    ["start"]=>
    int(0)
    ["end"]=>
    int(31865)
  }
  ["higher"]=>
  array(3) {
    ["rate"]=>
    int(40)
    ["start"]=>
    int(31866)
    ["end"]=>
    int(150000)
  }
  ["additional"]=>
  array(3) {
    ["rate"]=>
    int(45)
    ["start"]=>
    int(150001)
    ["end"]=>
    NULL
  }
}

I need to loop through not only the keys "basic", "higher" and "additional" but the arrays within them too.

I'll be comparing the "start" and "end" values with another variable and applying a calculation based on the "rate".

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.

Example:

foreach ($this->taxBand as $key => $band) {

            foreach ($band as $subKey => $value) {

                    // Do my stuff

            }

        }

If I return $band, I get:

array(5) {
  ["rate"]=>
  int(20)
  ["start"]=>
  int(0)
  ["end"]=>
  int(31865)
}

And $key returns:

string(5) "basic"

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.

Any help would be much appreciated. :)

EDIT: Trying to show an example of how I plan to use this loop. It's difficult because of the other functions/variables:

foreach ($this->taxBand as $key => $band) {

                    if ($band["end"] !== null || $band["end"] > 0) {
                        $band["amount"] = $this->get_lower_figure($this->totalTaxableAmount, $band["end"]) - $bandDeductions;
                    } else {
                        $band["amount"] = $this->totalTaxableAmount - $bandDeductions;
                    }

                $band["percentage_amount"] = ($band["amount"] / 100) * $band["rate"];
                $totalDeduction += $band["percentage_amount"];
                $bandDeductions += $band["amount"];

                return $totalDeduction;

        }

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.

get_lower_figure() just takes two arguments and checks which is less than the other.

5
  • 1
    What do you mean by "If I return $band, I get:..." The value of both $key and $band will change in each itteration of the loop, so $key will equal 'basic' then 'higher' and finaly 'aditional'. Please describe what you want to acheieve Commented Apr 2, 2014 at 15:06
  • Perhaps you dont need a loop at all, if you just want to access the values, eg: echo $this->taxBand['basic']['rate']; //outputs 20 Commented Apr 2, 2014 at 15:13
  • What I'm trying to achieve is check each band to see whether another $variable is between the start and end numbers for each of basic, higher, and additional. If it is, I need to do a calculation and output the return values for each of the bands. Commented Apr 2, 2014 at 15:14
  • 1
    OK, well then you probably do want a loop - Sams answer will guide you. If you still cant get it, please post an example code so others can check whats wrong Commented Apr 2, 2014 at 15:17
  • I added a better example (hopefully). Commented Apr 2, 2014 at 15:25

1 Answer 1

5

You seem to be going about everything right..but if you are going to be using the start/end/rate values directly, you shouldn't need the second foreach loop. Something like this should get you going:

$values = array();
foreach($this->taxBand as $key => $band) {
    $calculation = ($band['end'] - $band['start']) / $band['rate'];

    $values[$key] = $calculation;
}
return $values;

I'm doubting this is the work you plan to do, but the point is you can access the inner associative array $band directly.

If you were to use a sub-loop for whatever reason, you could do something like this:

foreach($this->taxBand as $key => $band) {
    // Set variables for this band

    foreach($band as $subKey => $value) {
        switch($subKey) {
             case 'start': // do something
                 break;
             case 'end': // do something
                 break;
             case 'rate': // do something
                 break;
        }
    }

    // Calculations finished
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is looking like it might be on the right track. I've added a hopefully better example so you can see what I intend to do with the inner array and what I need to output. :)
@IanNuttall you don't want to use return $var; in your loop. That will only return the first value. Instead, save the values to an array (so Array('basic' => 6373, 'higher' => 3254)) and return the array. I updated my first code block with a minor example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.