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.