0
0
PHPprogramming~5 mins

Array filter function in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 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.