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

minimum
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";
  1. $minimum ← 4, $matches ← 0, $total ← 0

    1<?php2$minimum→ 4 = 4; //@minimum=2, 63$matches→ 0 = 0;4$total→ 0 = 0;
  2. foreach (["left" => 3, "middle" => 5, "right" => 7] as $name => $amoun…

    pass 1 of 3
    6foreach (["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$total
    13left
    25middle40 10 5
    37right41 25 12
  3. $matches ← 1, $total ← 5

    pass 1 of 2
    6foreach (["left" => 3, "middle" => 5, "right" => 7] as $name => $amount) {7    if ($amount5 >= $minimum4) {8        $matches→ 1 = $matches + 1;9        $total→ 5 = $total + $amount5;10    }
  4. $matches ← 2, $total ← 12

    pass 2 of 2
    6foreach (["left" => 3, "middle" => 5, "right" => 7] as $name => $amount) {7    if ($amount7 >= $minimum4) {8        $matches→ 2 = $matches + 1;9        $total→ 12 = $total + $amount7;10    }
  5. echo "minimum=" . $minimum . " ";

    13echo "minimum=" . $minimum4 . "\n";14echo "matches=" . $matches2 . "\n";15echo "total=" . $total12 . "\n";
    outputminimum=4
    matches=2
    total=12
  1. $minimum ← 2, $matches ← 0, $total ← 0

    1<?php2$minimum→ 2 = 2;3$matches→ 0 = 0;4$total→ 0 = 0;
  2. foreach (["left" => 3, "middle" => 5, "right" => 7] as $name => $amoun…

    pass 1 of 3
    6foreach (["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
    13left
    25middle
    37right
  3. $matches ← 1, $total ← 3

    pass 1 of 3
    6foreach (["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$total
    130 10 3
    251 23 8
    372 38 15
  4. echo "minimum=" . $minimum . " ";

    13echo "minimum=" . $minimum2 . "\n";14echo "matches=" . $matches3 . "\n";15echo "total=" . $total15 . "\n";
    outputminimum=2
    matches=3
    total=15
  1. $minimum ← 6, $matches ← 0, $total ← 0

    1<?php2$minimum→ 6 = 6;3$matches→ 0 = 0;4$total→ 0 = 0;
  2. foreach (["left" => 3, "middle" => 5, "right" => 7] as $name => $amoun…

    pass 1 of 3
    6foreach (["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$total
    13left
    25middle
    37right60 10 7
  3. $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    }
  4. echo "minimum=" . $minimum . " ";

    13echo "minimum=" . $minimum6 . "\n";14echo "matches=" . $matches1 . "\n";15echo "total=" . $total7 . "\n";
    outputminimum=6
    matches=1
    total=7

Check Each Entry

  1. The array has names as keys and amounts as values.
  2. foreach gives the loop both $name and $amount.
  3. The if checks whether the amount reaches minimum.
  4. Matching entries increase both matches and total. | 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