We declare an array of 4 integers and then filter it with array_filter. We want to remove elements that are less than 3, so the result has just 3 and 4.
function filter_test($x) {
// Used in an array_filter call.
return $x >= 3;
}
$values = [1, 2, 3, 4];
// Version 1: use fn with fat arrow to filter array.
$result = array_filter($values, fn ($x) => $x >= 3);
var_dump($result);
// Version 2: use anonymous function to filter array.
$result = array_filter($values, function($x) {
return $x >= 3;
});
var_dump($result);
// Version 3: use named function.
$result = array_filter($values,
"filter_test");
var_dump($result);
array(2) {
[2]=>
int(3)
[3]=>
int(4)
}
array(2) {
[2]=>
int(3)
[3]=>
int(4)
}
array(2) {
[2]=>
int(3)
[3]=>
int(4)
}