Functional Helpers
Filter Values
array_filter keeps only the values accepted by a callback.
keep matching values
Filtering is useful when the callback can answer yes or no for each value.
Filter Values
filter_values.php
Replay: real traced execution (multi-file project)
<?php
function is_even($value) {
return $value % 2 === 0;
}
$extra = 5;
$values = [2, $extra, 6];
$kept = array_filter($values, "is_even");
$summary = implode(",", $kept);
$count = count($kept);
echo "extra=" . $extra . "\n";
echo "count=" . $count . "\n";
echo "kept=" . $summary . "\n";
<?php
function is_even($value) {
return $value % 2 === 0;
}
$extra = 1;
$values = [2, $extra, 6];
$kept = array_filter($values, "is_even");
$summary = implode(",", $kept);
$count = count($kept);
echo "extra=" . $extra . "\n";
echo "count=" . $count . "\n";
echo "kept=" . $summary . "\n";
<?php
function is_even($value) {
return $value % 2 === 0;
}
$extra = 8;
$values = [2, $extra, 6];
$kept = array_filter($values, "is_even");
$summary = implode(",", $kept);
$count = count($kept);
echo "extra=" . $extra . "\n";
echo "count=" . $count . "\n";
echo "kept=" . $summary . "\n";
$extra ← 5, $values ← Array
6$extra→ 5 = 5; //@extra=1, 87$values→ Array = [2, $extra5, 6];8$kept = array_filter($valuesArray, "is_even");9$summary = implode(",", $kept);function is_even($value)
pass 1 of 31<?php2function is_even($value2) {3 return $value2 % 2 === 0;4}All 3 passes — pass 1 is the card above pass $value1 2 2 5 3 6 $kept ← Array, $summary ← 2,6, $count ← 2
7$values = [2, $extra, 6];8$kept→ Array = array_filter($valuesArray, "is_even");9$summary→ 2,6 = implode(",", $keptArray);10$count→ 2 = count($keptArray);1112echo "extra=" . $extra5 . "\n";13echo "count=" . $count2 . "\n";14echo "kept=" . $summary2,6 . "\n";outputextra=5 count=2 kept=2,6
$extra ← 1, $values ← Array
6$extra→ 1 = 1;7$values→ Array = [2, $extra1, 6];8$kept = array_filter($valuesArray, "is_even");9$summary = implode(",", $kept);function is_even($value)
pass 1 of 31<?php2function is_even($value2) {3 return $value2 % 2 === 0;4}All 3 passes — pass 1 is the card above pass $value1 2 2 1 3 6 $kept ← Array, $summary ← 2,6, $count ← 2
7$values = [2, $extra, 6];8$kept→ Array = array_filter($valuesArray, "is_even");9$summary→ 2,6 = implode(",", $keptArray);10$count→ 2 = count($keptArray);1112echo "extra=" . $extra1 . "\n";13echo "count=" . $count2 . "\n";14echo "kept=" . $summary2,6 . "\n";outputextra=1 count=2 kept=2,6
$extra ← 8, $values ← Array
6$extra→ 8 = 8;7$values→ Array = [2, $extra8, 6];8$kept = array_filter($valuesArray, "is_even");9$summary = implode(",", $kept);function is_even($value)
pass 1 of 31<?php2function is_even($value2) {3 return $value2 % 2 === 0;4}All 3 passes — pass 1 is the card above pass $value1 2 2 8 3 6 $kept ← Array, $summary ← 2,8,6, $count ← 3
7$values = [2, $extra, 6];8$kept→ Array = array_filter($valuesArray, "is_even");9$summary→ 2,8,6 = implode(",", $keptArray);10$count→ 3 = count($keptArray);1112echo "extra=" . $extra8 . "\n";13echo "count=" . $count3 . "\n";14echo "kept=" . $summary2,8,6 . "\n";outputextra=8 count=3 kept=2,8,6