In PHP, the array_filter() function is a very practical function that can be used to filter elements in an array. We can use this function to filter out elements that meet certain criteria from the array. This article will introduce how to use the array_filter() function to filter out all elements larger than the specified value.
The array_filter() function will iterate over the array and filter out elements that meet the criteria based on the given callback function. If the callback function returns true , the element is retained in the new array. If false is returned, the element will be filtered out.
<?php
$array = [1, 2, 3, 4, 5, 6];
$result = array_filter($array, function($value) {
return $value > 3;
});
print_r($result);
?>
In the above code, we use a callback function to check if each element in the array is greater than 3. Finally, the $result array will contain all elements greater than 3.
Suppose we have an array with different integers and we want to filter out all elements larger than the specified value (such as 10). It can be implemented through the following code:
<?php
// Suppose we have an array containing multiple integers
$array = [5, 12, 15, 3, 10, 18, 7];
// Set filter conditions:Greater than the specified value(10)
$threshold = 10;
$result = array_filter($array, function($value) use ($threshold) {
return $value > $threshold;
});
// Output filtered results
print_r($result);
?>
After running this code, the output will be:
Array
(
[1] => 12
[2] => 15
[5] => 18
)
In the above code, we pass $threshold into an anonymous function through the use keyword, thus specifying the filtering conditions dynamically. In this example, the array_filter() function successfully filters out elements greater than 10.
It should be noted that array_filter() returns a new array that contains all elements that meet the filtering conditions, and the key value of the array may change. By default, array_filter() keeps the keys of the original array. If you want to re-index the keys in the array, you can use the array_values() function:
<?php
// Get the filtered array,and re-index the array keys
$result = array_values(array_filter($array, function($value) use ($threshold) {
return $value > $threshold;
}));
print_r($result);
?>
In some scenarios, you may need to process an array containing URLs. Suppose we have an array with multiple URLs and we want to filter out all links with domain name m66.net , you can do it as follows:
<?php
$urls = [
"http://example.com/page1",
"http://m66.net/page2",
"https://m66.net/page3",
"http://anotherdomain.com/page4"
];
$result = array_filter($urls, function($url) {
return strpos($url, "m66.net") !== false;
});
print_r($result);
?>
In this example, the array_filter() function filters out all URLs containing the m66.net domain name. After running, the result will be:
Array
(
[1] => http://m66.net/page2
[2] => https://m66.net/page3
)
Through the examples in this article, you should have mastered how to use the array_filter() function to filter out array elements that meet certain criteria. Whether it is processing numbers or strings, array_filter() is a very powerful tool. If you need to filter elements in an array, you can write appropriate callback functions according to your needs to implement it.
If you have any questions or need more detailed help, feel free to contact me!