• Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
logo logo
  • Home
  • Cloud VPS
    • Hong Kong VPS
    • US VPS
  • Dedicated Servers
    • Hong Kong Servers
    • US Servers
    • Singapore Servers
    • Japan Servers
  • Company
    • Contact Us
    • Blog
ENEN
  • 简体简体
  • 繁體繁體
Client Area

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

December 19, 2023

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.

Recent Posts

  • How to Set Up Nginx on CentOS Stream for High-Performance Web Hosting
  • CentOS Stream Explained: Key Differences from CentOS Linux
  • How to Configure FirewallD in CentOS Stream: From Essential to Production-Grade
  • Installing Docker on CentOS: A Practical Setup Guide (CentOS Stream 9/10 – 2026)
  • How to Secure a CentOS Server: 15 Essential Hardening Techniques (CentOS Stream 9/10 – 2026)

Recent Comments

No comments to show.

Knowledge Base

Access detailed guides, tutorials, and resources.

Live Chat

Get instant help 24/7 from our support team.

Send Ticket

Our team typically responds within 10 minutes.

logo
Alipay Cc-paypal Cc-stripe Cc-visa Cc-mastercard Bitcoin
Cloud VPS
  • Hong Kong VPS
  • US VPS
Dedicated Servers
  • Hong Kong Servers
  • US Servers
  • Singapore Servers
  • Japan Servers
More
  • Contact Us
  • Blog
  • Legal
© 2026 Server.HK | Hosting Limited, Hong Kong | Company Registration No. 77008912
Telegram
Telegram @ServerHKBot