I have multiple dimension arrays containing various values and would like to bring up the appropriate array or $profile
based of the users input into a HTML form.
I have started using array_map
, array_filter
and closure but I'm am only new to them all so I would very much appreciate an explanation alongside your code solution to help me learn. I know there are a lot of similar questions to this but I can't seem to make sense of them.
<?php
$profileArray = array(
array( 'Name' => "Toby",
'Age' => 3,
'Gender' => "Male",
),
array( 'Name' => "Cassie",
'Age' => 3,
'Gender' => "Female",
),
array( 'Name' => "Lucy",
'Age' => 1,
'Gender' => "Female",
),
);
$profiles = $profileArray[1][2][3];
class profileFilter {
function get_profile_by_age ($profiles, $age){
return array_filter ($profiles, function($data) use ($age){
return $data->age === $age;
});
}
}
var_dump (get_profile_by_age ($profiles, 3));
When I try this in my browser I get a syntax error on var_dump
EDIT: I have fixed suggested syntax errors but still no luck. Am I calling my array correctly? I feel like I am missing a step or syntax there too.
// meaningless line
// $profiles = $profileArray[1][2][3];
// i don't understand for what purpose you create a class,
// but if do, declare function as static
// or create an object and call it by obj->function
class profileFilter {
static function get_profile_by_age ($profile, $age){
return array_filter ($profile, function($data) use ($age){
// $data is arrray of arrays, there is no objects there
return $data['Age'] === $age;
});
}
}
var_dump (profileFilter::get_profile_by_age ($profileArray , 3));
// now it works