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
band) {
$calculation = ($band['end'] - $band['start']) / $band['rate'];
$values[
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
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
}
Answer from Sam on Stack OverflowCodecademy
codecademy.com › forum_questions › 556d9d0ad3292f03fb000558
7/7 How to iterate through a multidimensional associative array? | Codecademy
<?php // On the line below, create your own associative array: $myArray=array(array('Ball'=>'blue','Triangle'=>'green'), array('Cube'=> 'red','Cylinder'=>'pink')); // On the line below, output one of the values to the page: foreach($myArray as $key=>$value) { echo $value . " ". $value. "<br/><br/>"; } // On the line below, loop through the array and output echo "<br/>" ?> ... Think of multidimensional arrays as tables.
08:13
PHP Associative Arrays: The Ultimate Beginner's Guide - YouTube
18:20
Mastering PHP Arrays: Indexed, Associative & Multi-Dimensional ...
16:52
PHP Array Data Type - Indexed, Associative & Multi-Dimensional ...
03:47
PHP Multidimensional Arrays: Deep Dive for Beginners - YouTube
08:33
PHP Multidimensional Array Using Nested foreach loop - YouTube
04:59
How to loop through a Multidimensional Array in PHP | PHP ...
W3Schools
w3schools.com › php › php_arrays_multidimensional.asp
PHP Multidimensional Arrays
To loop through a multidimensional array, use a for loop or a foreach loop.
Tutorial Republic
tutorialrepublic.com › faq › foreach-loop-through-multidimensional-array-in-php.php
Foreach Loop through Multidimensional Array in PHP
<?php // Multidimensional array $superheroes = array( "spider-man" => array( "name" => "Peter Parker", "email" => "peterparker@mail.com", ), "super-man" => array( "name" => "Clark Kent", "email" => "clarkkent@mail.com", ), "iron-man" => array( "name" => "Harry Potter", "email" => "harrypotter@mail.com", ) ); // Printing all the keys and values one by one $keys = array_keys($superheroes); for($i = 0; $i < count($superheroes); $i++) { echo $keys[$i] .
Top answer 1 of 7
43
Nest two foreach loops:
foreach ($array as
values) {
print "$i {\n";
foreach ($values as
value) {
print "
value\n";
}
print "}\n";
}
2 of 7
42
I know it's question necromancy, but iterating over Multidimensional arrays is easy with Spl Iterators
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
foreach($iterator as
value) {
echo
value.'<br />';
}
See
- http://php.net/manual/en/spl.iterators.php
sebhastian
sebhastian.com › php-foreach-multidimensional-array
How to loop through a multidimensional array using foreach in PHP | sebhastian
December 2, 2022 - <?php // Create a multidimensional associative array $members = [ "Front-end developer" => [ "name" => "Nathan", "age" => 29, ], "Back-end developer" => [ "name" => "Susan", "age" => 32, ], "Database engineer" =>[ "name" => "Jane", "age" => 23, ], ]; // use the same foreach loop foreach ($members as $index => $member) { print "Array index : {$index}"; print PHP_EOL; foreach ($member as $key => $value) { print "{$key} : {$value}"; print PHP_EOL; } }
PHP
php.net › manual › en › control-structures.foreach.php
PHP: foreach - Manual
echo "$a $b\n"; } foreach ($array as [, , $c]) { // Pulando $a e $b echo "$c\n"; } ?> I would like to correct this example above! The answer of this algorithm is: 1 2 3 4 3 6 ... As well as numerically-indexed arrays, nested associative arrays can also be unnested in the foreach() <?php $records = [ ['name' => 'Alice', 'address' => '...'], ['name' => 'Bob', 'address' => '...'], ['name' => 'Chris', 'address' => '...'], ...]; foreach($records as ['name' => $name]) { echo "$name\n"; } foreach($records as ['address' => $address, 'name' => $name]) { echo "$name @ $address\n"; } ?> The usual warnings about undefined array keys will occur if any of the element arrays lack the requested key.
Jobtensor
jobtensor.com › Tutorial › PHP › en › Arrays
PHP Arrays - Indexed, Associative, Multidimensional | jobtensor
Looping through an associative array can be done using a foreach loop. <?php $ages = array("Mark" => 22, "Jeff" => 32, "Mike" => 28); foreach($ages as $x => $x_value) { echo "Name = " . $x . ", Age = " . $x_value; echo "<br>"; } A multidimensional array is an array in which each element may ...
Stack Overflow
stackoverflow.com › questions › 32042106 › php-foreach-multidimensional-associative-array
loops - php foreach multidimensional associative array - Stack Overflow
Ideally, the structure of your array is consistent (ideally, a two-dimensional array of rows, each containing a set of fields), then a simple nested loop will work. ... Sign up to request clarification or add additional context in comments. ... Save this answer. ... Show activity on this post. Try the below solution with a combination of for()/foreach() and looped iteration.
Pi My Life Up
pimylifeup.com › home › how to use php foreach loops
How to Use PHP foreach Loops - Pi My Life Up
May 1, 2022 - There are two different ways you can write a foreach loop in PHP. Our first example iterates through an array assigning the current element’s value to the variable $value. You can use the $value variable anywhere within the foreach code block. foreach ($array as $value) { //code to execute }Copy · Our second example is for associative arrays, which will contain a range of keys.
GeeksforGeeks
geeksforgeeks.org › php › how-to-use-foreach-loop-with-multidimensional-arrays-in-php
How to Use Foreach Loop with Multidimensional Arrays in PHP? - GeeksforGeeks
July 23, 2025 - The basic syntax of a foreach loop in PHP is as follows: foreach ($array as $value) { // Code to be executed for each $value } To iterate through a multidimensional array using foreach loops, you can nest the loops for each level of the array.
Codecademy
codecademy.com › forum_questions › 52579398abf82173e4001ffe
How to use foreach() for associative multidimensional arrays? | Codecademy
$myArray = array(array('color'=>'pink'), array('food'=>'dumplings'), array('sauce'=>'chilli') ); // On the line below, output one of the values to the page: //echo $myArray[1]['food']; // On the line below, loop through the array and output // *all* of the values to the page: foreach ($myArray as $cynaras) { foreach($cynaras as $items) { echo $cynaras." favorite ".$items."<br>"; } }
Developer Drive
developerdrive.com › home › php arrays: array functions and multidimensional arrays
PHP Arrays: Array Functions and Multidimensional Arrays - Developer Drive
January 10, 2022 - For instance, the PHP code below: <? echo $arrBooks[‘Science Fiction][‘Title’]; echo "<br>"; echo $arrBooks[‘Horror’][‘Author’]; ?> would display: Dune Stephen King · The easiest way to loop through a multidimensional array is to nest two foreach loops; the outer loop goes through ...
Top answer 1 of 3
5
This should do it... (check the privilege level)
foreach($array as $level => $priv){
// check for privilege level
if($level >= $accessLevel){
// loop through privilege array
foreach($priv as $command => $list){
foreach($list as $trigger => $description)
}
}
}
}
On a side note, instead of using string keys for level you could use array indicies, and that would allow the combined outer foreach/if combination to be written as
for($i = $accessLevel; $i >= 0; $i--){
$priv = $array[$i];
//...
}
2 of 3
0
for ($i = 5; $i >= 0; --$i) {
//list commands for accesslevel $i
}