How can PHP be used to filter out elements from a multidimensional array based on specific criteria?
To filter out elements from a multidimensional array based on specific criteria in PHP, you can use the array_filter() function along with a custom callback function. The callback function should define the criteria for filtering the elements. It will be applied to each element of the array, and only the elements that meet the criteria will be retained in the filtered array.
// Sample multidimensional array
$students = [
['name' => 'John', 'age' => 20],
['name' => 'Alice', 'age' => 22],
['name' => 'Bob', 'age' => 18]
];
// Define the criteria for filtering (e.g., age greater than 20)
$filtered_students = array_filter($students, function($student) {
return $student['age'] > 20;
});
// Output the filtered array
print_r($filtered_students);