0
0
PHPprogramming~10 mins

Array filter function in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Array filter function
Start with array
Pick first element
Apply filter function
Keep element
Move to next element
More elements?
NoReturn filtered array
Back to Pick first element
The filter function checks each element with a test. If the test passes, it keeps the element; if not, it skips it. This repeats until all elements are checked.
Execution Sample
PHP
<?php
$numbers = [1, 2, 3, 4, 5];
$even = array_filter($numbers, fn($n) => $n % 2 === 0);
print_r($even);
?>
This code filters the array to keep only even numbers.
Execution Table
StepCurrent ElementCondition ($n % 2 === 0)Keep Element?Filtered Array So Far
111 % 2 === 0 → falseNo[]
222 % 2 === 0 → trueYes[2]
333 % 2 === 0 → falseNo[2]
444 % 2 === 0 → trueYes[2, 4]
555 % 2 === 0 → falseNo[2, 4]
💡 All elements checked, filter complete.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
$numbers[1,2,3,4,5][1,2,3,4,5][1,2,3,4,5][1,2,3,4,5][1,2,3,4,5][1,2,3,4,5][1,2,3,4,5]
$even[][][2][2][2,4][2,4][2,4]
Key Moments - 3 Insights
Why does the filtered array only contain some elements, not all?
Because the filter function tests each element. Only elements passing the condition (true) are kept, as shown in execution_table rows 2 and 4.
Does the original array change after filtering?
No, the original array ($numbers) stays the same throughout, as shown in variable_tracker where $numbers never changes.
What happens if the condition is false for all elements?
The filtered array will be empty because no elements pass the test, similar to execution_table rows where 'Keep Element?' is 'No'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the filtered array after step 4?
A[2, 3, 4]
B[2, 4]
C[1, 3, 5]
D[1, 2, 3, 4]
💡 Hint
Check the 'Filtered Array So Far' column at step 4 in the execution_table.
At which step does the condition first become true?
AStep 1
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'Condition' column in execution_table to find the first 'true' result.
If the filter condition was changed to keep odd numbers, what would be the filtered array after step 5?
A[1, 3, 5]
B[2, 4]
C[1, 2, 3, 4, 5]
D[]
💡 Hint
Think about which numbers are odd in the original array $numbers from variable_tracker.
Concept Snapshot
array_filter(array, callback) → returns new array
Checks each element with callback function
Keeps elements where callback returns true
Original array stays unchanged
Useful to select elements by condition
Full Transcript
The array_filter function in PHP takes an array and a callback function. It checks each element of the array with the callback. If the callback returns true, the element is kept in the new filtered array. If false, the element is skipped. This process repeats for all elements. The original array does not change. For example, filtering even numbers keeps only numbers divisible by 2. The filtered array builds up step by step as elements pass the test.