I don't understand how this thing works but I need to create a new function and store it in a variable. Then, by using array_filter
, remove all dogs that are under certain age. Here is what I created so far:
$animals = [
[ 'name' => 'Waffles', 'type' => 'dog', 'age' => 12],
[ 'name' => 'Fluffy', 'type' => 'cat', 'age' => 14],
[ 'name' => 'Spelunky', 'type' => 'dog', 'age' => 4],
[ 'name' => 'Hank' , 'type' => 'dog', 'age' => 11],
];
$youngDogs = function ($animals, $filter){
array_filter(
$animals,
function($animals, $age = 5){
$arr = [];
if($animals['type'] == 'dog' && $animals['age'] < $age)
{
$arr = [
'name' => $animals['name'],
'type' => $animals['type'],
'age' => $animals['age'],
];
}
return $arr;
}
);
};
var_dump($youngDogs($animals, 5));
My idea is to create a new array and store the dogs that match the criteria and return it, however everything is a mess and the function is returning NULL and I don't know what is happening anymore. Could anyone give me a hint?