Arrays and Iteration
Foreach Keys
Associative arrays let foreach read both the key and the value for each entry.
associative iteration
When iterating an associative array, the loop can receive both the key and its matching value.
Foreach Keys
foreach_keys.php
Replay: real traced execution (multi-file project)
<?php
$minimum = 4;
$matches = 0;
$total = 0;
foreach (["left" => 3, "middle" => 5, "right" => 7] as $name => $amount) {
if ($amount >= $minimum) {
$matches = $matches + 1;
$total = $total + $amount;
}
}
echo "minimum=" . $minimum . "\n";
echo "matches=" . $matches . "\n";
echo "total=" . $total . "\n";
<?php
$minimum = 2;
$matches = 0;
$total = 0;
foreach (["left" => 3, "middle" => 5, "right" => 7] as $name => $amount) {
if ($amount >= $minimum) {
$matches = $matches + 1;
$total = $total + $amount;
}
}
echo "minimum=" . $minimum . "\n";
echo "matches=" . $matches . "\n";
echo "total=" . $total . "\n";
<?php
$minimum = 6;
$matches = 0;
$total = 0;
foreach (["left" => 3, "middle" => 5, "right" => 7] as $name => $amount) {
if ($amount >= $minimum) {
$matches = $matches + 1;
$total = $total + $amount;
}
}
echo "minimum=" . $minimum . "\n";
echo "matches=" . $matches . "\n";
echo "total=" . $total . "\n";
$minimum ← 4, $matches ← 0, $total ← 0
1<?php2$minimum→ 4 = 4; //@minimum=2, 63$matches→ 0 = 0;4$total→ 0 = 0;foreach (["left" => 3, "middle" => 5, "right" => 7] as $name => $amoun…
pass 1 of 36foreach (["left" => 3, "middle" => 5, "right" => 7] as $nameleft => $amount3) {7 if ($amount >= $minimum) {8 $matches = $matches + 1;All 3 passes — pass 1 is the card above pass $amount$name$minimum$matches$total1 3 left — — — 2 5 middle 4 0 → 1 0 → 5 3 7 right 4 1 → 2 5 → 12 $matches ← 1, $total ← 5
pass 1 of 26foreach (["left" => 3, "middle" => 5, "right" => 7] as $name => $amount) {7 if ($amount5 >= $minimum4) {8 $matches→ 1 = $matches + 1;9 $total→ 5 = $total + $amount5;10 }$matches ← 2, $total ← 12
pass 2 of 26foreach (["left" => 3, "middle" => 5, "right" => 7] as $name => $amount) {7 if ($amount7 >= $minimum4) {8 $matches→ 2 = $matches + 1;9 $total→ 12 = $total + $amount7;10 }echo "minimum=" . $minimum . " ";
13echo "minimum=" . $minimum4 . "\n";14echo "matches=" . $matches2 . "\n";15echo "total=" . $total12 . "\n";outputminimum=4 matches=2 total=12
$minimum ← 2, $matches ← 0, $total ← 0
1<?php2$minimum→ 2 = 2;3$matches→ 0 = 0;4$total→ 0 = 0;foreach (["left" => 3, "middle" => 5, "right" => 7] as $name => $amoun…
pass 1 of 36foreach (["left" => 3, "middle" => 5, "right" => 7] as $nameleft => $amount3) {7 if ($amount >= $minimum) {8 $matches = $matches + 1;All 3 passes — pass 1 is the card above pass $amount$name1 3 left 2 5 middle 3 7 right $matches ← 1, $total ← 3
pass 1 of 36foreach (["left" => 3, "middle" => 5, "right" => 7] as $name => $amount) {7 if ($amount3 >= $minimum2) {8 $matches→ 1 = $matches + 1;9 $total→ 3 = $total + $amount3;10 }All 3 passes — pass 1 is the card above pass $amount$matches$total1 3 0 → 1 0 → 3 2 5 1 → 2 3 → 8 3 7 2 → 3 8 → 15 echo "minimum=" . $minimum . " ";
13echo "minimum=" . $minimum2 . "\n";14echo "matches=" . $matches3 . "\n";15echo "total=" . $total15 . "\n";outputminimum=2 matches=3 total=15
$minimum ← 6, $matches ← 0, $total ← 0
1<?php2$minimum→ 6 = 6;3$matches→ 0 = 0;4$total→ 0 = 0;foreach (["left" => 3, "middle" => 5, "right" => 7] as $name => $amoun…
pass 1 of 36foreach (["left" => 3, "middle" => 5, "right" => 7] as $nameleft => $amount3) {7 if ($amount >= $minimum) {8 $matches = $matches + 1;All 3 passes — pass 1 is the card above pass $amount$name$minimum$matches$total1 3 left — — — 2 5 middle — — — 3 7 right 6 0 → 1 0 → 7 $matches ← 1, $total ← 7
6foreach (["left" => 3, "middle" => 5, "right" => 7] as $name => $amount) {7 if ($amount7 >= $minimum6) {8 $matches→ 1 = $matches + 1;9 $total→ 7 = $total + $amount7;10 }echo "minimum=" . $minimum . " ";
13echo "minimum=" . $minimum6 . "\n";14echo "matches=" . $matches1 . "\n";15echo "total=" . $total7 . "\n";outputminimum=6 matches=1 total=7
Check Each Entry
- The array has names as keys and amounts as values.
foreachgives the loop both$nameand$amount.- The
ifchecks whether the amount reachesminimum. - Matching entries increase both
matchesandtotal. | Key | Amount |>= 4? | | --- | --- | --- | |left|3| no | |middle|5| yes | |right|7| yes |
Exercise: foreach_keys.php
Loop through named amounts, count entries at or above a minimum, and add their values