In PHP, array_filter() is a very common function that filters elements of an array based on a given callback function. It generates a new array by removing elements that do not meet the criteria. Although array_filter() is a concise tool, the readability of the code may be reduced without reasonable use. To improve the readability of the code, we can use ternary operators to simplify the writing of callback functions.
The array_filter() function accepts two parameters: an array and a callback function. It will iterate over each element in the array, retain the element whose return value of the callback function is true , and the element whose return value is false is filtered out.
<?php
$numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
$filteredNumbers = array_filter($numbers, function($number) {
return $number % 2 === 0; // Only even numbers are retained
});
print_r($filteredNumbers);
?>
The output result is:
Array
(
[1] => 2
[3] => 4
[5] => 6
[7] => 8
)
The ternary operator ( ? : ) is a concise conditional expression that allows us to make conditional judgments within a line. We can use ternary operators to simplify the logic of callback functions in array_filter() to make it more concise and easy to read. Especially when a value is required to be simple, the ternary operator can make the code more compact.
Suppose we want to filter an array, keeping all elements larger than 5. We can implement this logic through the ternary operator:
<?php
$numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
$filteredNumbers = array_filter($numbers, function($number) {
return $number > 5 ? true : false; // If greater than5,return true
});
print_r($filteredNumbers);
?>
The output result is:
Array
(
[5] => 6
[6] => 7
[7] => 8
[8] => 9
)
In this example, the ternary operator ? true: false is used to determine whether the condition of number > 5 is true. If it is true, it will return true , otherwise it will return false . Although return $number > 5; can also achieve the same effect, through the ternary operator we can keep the code concise when more complex judgments are needed.
Suppose we need to filter an array containing URLs and only URLs starting with http://m66.net are retained. In this case, we can use ternary operators to simplify the judgment logic.
<?php
$urls = [
'http://m66.net/page1',
'http://example.com/page2',
'http://m66.net/page3',
'http://anotherdomain.com/page4',
];
$filteredUrls = array_filter($urls, function($url) {
return strpos($url, 'http://m66.net') === 0 ? true : false; // Only retain m66.net The beginning URL
});
print_r($filteredUrls);
?>
The output result is:
Array
(
[0] => http://m66.net/page1
[2] => http://m66.net/page3
)
In this example, strpos($url, 'http://m66.net') === 0 is used to determine whether the URL starts with http://m66.net . The ternary operator ensures that we can handle this logic concisely.
When using array_filter() , the code readability and simplicity can be improved by introducing ternary operators, especially when the condition judgment in the callback function is simple, ternary operators can help us avoid lengthy if statements. At the same time, it also allows the code to maintain a concise structure when making more complex judgments.