What are some alternative approaches or more elegant solutions to the problem of filtering elements in a multidimensional array in PHP?
When filtering elements in a multidimensional array in PHP, one common approach is to use nested loops to iterate through the array and check each element against the filtering criteria. However, a more elegant solution is to use array_filter() function along with a custom callback function to achieve the same result in a more concise and readable way.
// Sample multidimensional array
$array = [
['name' => 'John', 'age' => 25],
['name' => 'Jane', 'age' => 30],
['name' => 'Alice', 'age' => 20]
];
// Filter elements where age is greater than 25
$filteredArray = array_filter($array, function($item) {
return $item['age'] > 25;
});
print_r($filteredArray);