Bird
Raised Fist0
PHPprogramming~5 mins

Array filter function in PHP - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does the array_filter() function do in PHP?
It filters elements of an array using a callback function, returning a new array with only the elements that pass the test.
Click to reveal answer
beginner
How do you use array_filter() without a callback function?
It removes all elements that are considered false (like 0, '', null, false) and returns the rest.
Click to reveal answer
beginner
What type of argument does the callback function in array_filter() receive?
The callback function receives each element of the array as its argument to test if it should be included.
Click to reveal answer
intermediate
Does array_filter() preserve the original array keys?
Yes, it preserves the keys of the original array in the filtered result.
Click to reveal answer
intermediate
How can you reindex the array returned by array_filter()?
Use the array_values() function to reset the keys to consecutive numbers starting from 0.
Click to reveal answer
What does array_filter() return when no callback is provided?
AAn empty array
BThe original array unchanged
CAn array with all elements except those that are falsey
DAn array with only falsey elements
Which of these is a valid callback for array_filter()?
AA function that returns true or false based on the element
BA function that returns the element itself
CA function that returns a string
DA function that returns an integer index
Does array_filter() change the keys of the original array by default?
AYes, it resets keys to 0-based indexes
BNo, it preserves the original keys
CIt removes keys completely
DIt converts keys to strings
How do you remove all empty strings from an array using array_filter()?
AUse <code>array_filter($array, fn($v) =&gt; $v !== '')</code>
BUse <code>array_filter($array, fn($v) =&gt; is_null($v))</code>
CUse <code>array_filter($array, fn($v) =&gt; $v === '')</code>
DUse <code>array_filter($array)</code> without a callback
What function can you use to reindex an array after filtering?
Aarray_keys()
Barray_merge()
Carray_slice()
Darray_values()
Explain how array_filter() works and give an example of filtering out falsey values.
Think about how you pick only certain items from a list based on a test.
You got /4 concepts.
    Describe how to preserve or reset keys when using array_filter().
    Consider what happens to the numbering of items after filtering.
    You got /3 concepts.

      Practice

      (1/5)
      1. What does the PHP function array_filter do to an array?
      easy
      A. It removes all keys from the array.
      B. It keeps only the elements where the callback returns true.
      C. It changes all values to strings.
      D. It sorts the array in ascending order.

      Solution

      1. Step 1: Understand the purpose of array_filter

        The function array_filter tests each element with a callback function.
      2. Step 2: Determine what happens based on callback result

        Elements for which the callback returns true are kept; others are removed.
      3. Final Answer:

        It keeps only the elements where the callback returns true. -> Option B
      4. Quick Check:

        array_filter keeps elements with true callback [OK]
      Hint: Remember: true keeps, false removes elements [OK]
      Common Mistakes:
      • Thinking array_filter sorts the array
      • Assuming it changes values to strings
      • Believing it removes keys
      2. Which of the following is the correct syntax to use array_filter with a callback function named isPositive on array $numbers?
      easy
      A. array_filter($numbers, isPositive);
      B. array_filter($numbers, &isPositive);
      C. array_filter(isPositive, $numbers);
      D. array_filter($numbers, 'isPositive');

      Solution

      1. Step 1: Recall array_filter syntax

        The second argument must be a callable, passed as a string or anonymous function.
      2. Step 2: Identify correct callable format

        Passing the function name as a string, like 'isPositive', is correct.
      3. Final Answer:

        array_filter($numbers, 'isPositive'); -> Option D
      4. Quick Check:

        Callback function name as string [OK]
      Hint: Callback function names go in quotes [OK]
      Common Mistakes:
      • Passing callback without quotes
      • Swapping argument order
      • Using & before function name
      3. What is the output of the following PHP code?
      $arr = [1, 2, 3, 4, 5];
      $result = array_filter($arr, fn($n) => $n % 2 === 0);
      print_r($result);
      medium
      A. Array ( [0] => 2 [1] => 4 )
      B. Array ( [0] => 1 [2] => 3 [4] => 5 )
      C. Array ( [1] => 2 [3] => 4 )
      D. Array ( [0] => 2 [2] => 4 )

      Solution

      1. Step 1: Understand the callback function

        The callback keeps numbers where $n % 2 === 0, meaning even numbers.
      2. Step 2: Identify even numbers and keys

        Even numbers in the array are 2 and 4 at keys 1 and 3 respectively.
      3. Final Answer:

        Array ( [1] => 2 [3] => 4 ) -> Option C
      4. Quick Check:

        Only even numbers kept with original keys [OK]
      Hint: Check which elements return true, keys stay same [OK]
      Common Mistakes:
      • Assuming keys are reindexed
      • Keeping odd numbers by mistake
      • Confusing array keys with values
      4. Identify the error in this PHP code using array_filter:
      $data = [10, 15, 20];
      $result = array_filter($data, function($x) {
        if ($x > 15) return true;
      });
      print_r($result);
      medium
      A. The callback function does not return false for some values.
      B. array_filter requires a third argument for keys.
      C. The array must be associative for array_filter to work.
      D. The print_r function cannot print arrays.

      Solution

      1. Step 1: Check callback return values

        The callback returns true only if $x > 15, but returns nothing (null) otherwise.
      2. Step 2: Understand array_filter behavior

        Callback must return true or false explicitly; null is treated as false but is unclear and risky.
      3. Final Answer:

        The callback function does not return false for some values. -> Option A
      4. Quick Check:

        Callback must return true or false explicitly [OK]
      Hint: Always return true or false in callback [OK]
      Common Mistakes:
      • Missing explicit false return
      • Thinking array_filter needs 3 arguments
      • Believing print_r can't print arrays
      5. Given an array $items = ['apple' => 3, 'banana' => 0, 'cherry' => 5, 'date' => null];, which array_filter call will keep only items with values that are positive numbers (greater than zero)?
      hard
      A. array_filter($items, fn($v) => $v > 0);
      B. array_filter($items, fn($v) => (bool)$v);
      C. array_filter($items);
      D. array_filter($items, fn($v) => $v !== 0);

      Solution

      1. Step 1: Understand the goal

        We want to keep only values greater than zero (positive numbers).
      2. Step 2: Analyze each option

        array_filter($items, fn($v) => $v > 0); keeps values where $v > 0, exactly what we want.
        array_filter($items, fn($v) => (bool)$v); keeps all truthy values, but 0 is false and null is false, so it removes them but also keeps negative numbers if any.
        array_filter($items); uses default filter which removes falsy values (0, null, false), but does not check for > 0.
        array_filter($items, fn($v) => $v !== 0); removes only zeros but keeps null and negative numbers.
      3. Final Answer:

        array_filter($items, fn($v) => $v > 0); -> Option A
      4. Quick Check:

        Filter with $v > 0 keeps only positive numbers [OK]
      Hint: Use explicit condition for positive numbers [OK]
      Common Mistakes:
      • Using default filter which keeps all truthy values
      • Filtering only non-zero but allowing null
      • Confusing truthy with positive
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard