How to handle associative arrays using array_filter() function? How does it work and what problems can it solve?
In PHP, the array_filter() function is a very useful tool for filtering array elements. It filters elements in the array based on the incoming callback function, and only elements that meet the conditions are retained. If the callback function is not passed, array_filter() will delete the element with a value of false . When processing associative arrays, array_filter() can also perform some complex data filtering operations very conveniently. Next, we will explain in detail how to use array_filter() to handle associative arrays, as well as how it works and application scenarios.
array array_filter ( array $array , callable $callback = ? , int $mode = 0 ) : array
$array : an array to be filtered.
$callback : A callback function for filtering. If the callback function is not passed, the element with the value of false will be deleted by default.
$mode : Determines whether the parameter of the callback function is the value of the array element or the key-value pair of the array. ARRAY_FILTER_USE_KEY (default value), ARRAY_FILTER_USE_BOTH (pass both keys and values).
In PHP, the keys to the associative array are usually strings or numbers, and the paired values are the data you want to store and filter. array_filter() can be filtered based on values, keys, or both keys and values.
Suppose we have an associative array containing multiple user information, and we want to filter out all users who are younger than 18.
<?php
$users = [
"user1" => ["name" => "Alice", "age" => 25],
"user2" => ["name" => "Bob", "age" => 17],
"user3" => ["name" => "Charlie", "age" => 30]
];
$filtered_users = array_filter($users, function($user) {
return $user["age"] >= 18; // Only age is greater than or equal to 18 Users
});
print_r($filtered_users);
?>
Output:
Array
(
[user1] => Array
(
[name] => Alice
[age] => 25
)
[user3] => Array
(
[name] => Charlie
[age] => 30
)
)
In the example above, the array_filter() function traverses the array $users and uses the callback function to filter out users older than or equal to 18.
Suppose we want to filter associative arrays based on certain keys, such as deleting information from certain users.
<?php
$users = [
"user1" => ["name" => "Alice", "age" => 25],
"user2" => ["name" => "Bob", "age" => 17],
"user3" => ["name" => "Charlie", "age" => 30]
];
$filtered_users = array_filter($users, function($user, $key) {
return $key != "user2"; // The exclusion key is user2 Users
}, ARRAY_FILTER_USE_BOTH);
print_r($filtered_users);
?>
Output:
Array
(
[user1] => Array
(
[name] => Alice
[age] => 25
)
[user3] => Array
(
[name] => Charlie
[age] => 30
)
)
In this example, array_filter() uses the ARRAY_FILTER_USE_BOTH mode, so that the callback function can access not only the values of array elements, but also their keys. This way, we can decide whether to retain the element based on the key.
When processing data, you may encounter an array containing a null value, null , false , 0 or empty string. Use array_filter() to clean up these invalid data easily.
For example, delete an element with a value of false in an array:
<?php
$array = [0, 1, false, 2, "", null, 3];
$filtered_array = array_filter($array); // All will be deleted by default `false` value
print_r($filtered_array);
?>
Output:
Array
(
[1] => 1
[3] => 2
[6] => 3
)
array_filter() is a very convenient tool when you need to filter out elements that meet certain criteria from a dataset. For example, it can be used to filter out all orders with an amount greater than a certain value, or to filter out all online users.
Complex data filtering logic can be implemented with cleaner code through array_filter() . Compared to using foreach loops to manually filter data, array_filter() makes the code clearer and easier to read.
Suppose we have an array containing multiple URL addresses and we want to filter out URLs containing the specified domain name (e.g. m66.net ). We can use array_filter() to cooperate with parse_url() to complete this task.
<?php
$urls = [
"http://example.com/page1",
"https://m66.net/page2",
"https://m66.net/page3",
"http://anotherdomain.com/page4"
];
$filtered_urls = array_filter($urls, function($url) {
$parsed_url = parse_url($url);
return isset($parsed_url['host']) && $parsed_url['host'] === 'm66.net';
});
print_r($filtered_urls);
?>
Output: