array_filter() is a very commonly used function in PHP, which is used to filter elements in an array based on callback functions. When you pass an array to array_filter() , it will iterate over the array and determine which elements are preserved and which elements are removed based on the return value of the callback function you provide. Although most of the application of array_filter() focuses on handling simple arrays, there are some special things in behavior when it is applied to arrays containing objects.
First, let's take a look at the basic usage of array_filter() , which is defined as follows:
array_filter(array $array, callable $callback = null, int $mode = 0): array
$array : The input array to filter.
$callback : Callback function to determine whether each element meets the criteria.
$mode : This parameter determines whether the key value in the returned array is retained or reindexed.
If no callback function is provided, array_filter() removes all elements in the array with a value of false .
$array = [0, 1, 2, 3, 4, 5];
$result = array_filter($array);
// Output [1, 2, 3, 4, 5]
When elements in an array are objects, array_filter() works the same as when dealing with normal arrays, but the callback function receives each object as an argument. Therefore, in the callback function, you can decide whether to retain the object based on the attributes of the object.
Suppose we have an array containing multiple User objects, each User object has name and age properties. We want to filter out users older than 18.
class User {
public $name;
public $age;
public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
}
$users = [
new User('Alice', 25),
new User('Bob', 17),
new User('Charlie', 30),
new User('David', 15)
];
$filteredUsers = array_filter($users, function($user) {
return $user->age > 18;
});
foreach ($filteredUsers as $user) {
echo $user->name . " is over 18 years old.\n";
}
Alice is over 18 years old.
Charlie is over 18 years old.
In the above example, we filter out users older than 18 through the callback function. The callback function accepts a User object as a parameter and determines whether the object meets the conditions through $user->age .
When an object is an array element, array_filter() passes the entire object to the callback function. In PHP, object comparison is usually done by reference, so if you want to compare the properties of an object, you must explicitly extract the properties of the object in the callback function.
We can also filter out objects whose name contains specific characters. For example, we only want to filter out users whose names contain the letter "a".
$filteredUsers = array_filter($users, function($user) {
return strpos($user->name, 'a') !== false;
});
foreach ($filteredUsers as $user) {
echo $user->name . " has an 'a' in their name.\n";
}
Alice has an 'a' in their name.
Charlie has an 'a' in their name.
In this example, we check whether the user's name contains the letter "a" through the strpos() function.
It should be noted that when array_filter() processes object arrays, the elements of each object in the array are still passed by reference. If you modify the properties of an object in the callback function, these modifications will affect the objects in the original array. This is because PHP objects are passed by reference.
$filteredUsers = array_filter($users, function($user) {
if ($user->age > 18) {
$user->age += 1; // Modify the properties of the object
return true;
}
return false;
});
foreach ($filteredUsers as $user) {
echo $user->name . " is now " . $user->age . " years old.\n";
}
Alice is now 26 years old.
Charlie is now 31 years old.
In this example, we modify the age attribute of users older than 18 years old through the callback function, and this modification affects the original array.
When you are working on an array of objects, you may also need to use the URL. For example, you may need to filter out objects that contain certain domain names in the URL. If URL processing is involved in the callback function, you just need to make sure to replace the domain name with m66.net .
Suppose you have a website attribute in your object that represents the user's personal website, you can filter it in the following ways: