PHP · December 19, 2023

Php Tip: Use array_filter() to filters elements of an array using a callback function

Php Tip: Use array_filter() to filters elements of an array using a callback function

When working with arrays in PHP, it is often necessary to filter out certain elements based on specific criteria. The array_filter() function provides a convenient way to accomplish this task by allowing you to apply a callback function to each element of the array and keep only those that meet the specified conditions.

How does array_filter() work?

The array_filter() function takes two parameters: the input array and the callback function. The callback function is applied to each element of the input array, and if it returns true, the element is included in the resulting filtered array. If the callback function returns false, the element is excluded.

Here's the basic syntax of the array_filter() function:

$filtered_array = array_filter($input_array, $callback_function);

Let's dive into an example to better understand how to use array_filter().

Example: Filtering an array of numbers

Suppose we have an array of numbers and we want to filter out all the even numbers. We can achieve this using array_filter() with a callback function that checks if a number is even.

$numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

$filtered_numbers = array_filter($numbers, function($number) {
    return $number % 2 == 0;
});

print_r($filtered_numbers);

The output of the above code will be:

Array
(
    [1] => 2
    [3] => 4
    [5] => 6
    [7] => 8
    [9] => 10
)

As you can see, the resulting array only contains the even numbers from the original array.

Using array_filter() with associative arrays

array_filter() can also be used with associative arrays. In this case, the keys of the original array are preserved in the resulting filtered array.

Let's consider an example where we have an associative array of students and we want to filter out all the students whose age is less than 18.

$students = [
    'John' => 20,
    'Jane' => 17,
    'Mike' => 19,
    'Sarah' => 16
];

$filtered_students = array_filter($students, function($age) {
    return $age < 18;
});

print_r($filtered_students);

The output will be:

Array
(
    [Jane] => 17
    [Sarah] => 16
)

In this case, only the students with an age less than 18 are included in the resulting array.

Conclusion

The array_filter() function is a powerful tool in PHP for filtering elements of an array based on specific conditions. It allows you to easily create custom filtering logic using callback functions. By leveraging array_filter(), you can efficiently manipulate arrays and extract the desired elements.

Summary

In summary, the array_filter() function in PHP is a useful tool for filtering elements of an array based on specific conditions. It takes an input array and a callback function as parameters, and returns a new array containing only the elements that satisfy the callback function. By using array_filter(), you can efficiently manipulate arrays and extract the desired elements. To learn more about PHP and VPS hosting, visit Server.HK.