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?✗ Incorrect
Without a callback,
array_filter() removes falsey values like 0, '', false, and null.Which of these is a valid callback for
array_filter()?✗ Incorrect
The callback must return true to keep the element or false to remove it.
Does
array_filter() change the keys of the original array by default?✗ Incorrect
array_filter() keeps the original keys unless you reindex manually.How do you remove all empty strings from an array using
array_filter()?✗ Incorrect
The callback keeps elements that are not empty strings.
What function can you use to reindex an array after filtering?
✗ Incorrect
array_values() resets keys to consecutive numbers starting at 0.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.