In the following array I would like to search for firstname, e.g tom and then echo out the name of the key "library" and "canteen" because there is stored a person with first name tom in both of them.
This is the output of print_r($school);
Array
(
[library] => Array
(
[0] => Array
(
[firstname] => tom
[lastname] => brown
)
)
)
Array
(
[canteen] => Array
(
[0] => Array
(
[firstname] => matt
[lastname] => smith
)
[1] => Array
(
[firstname] => tom
[lastname] => jones
)
)
)
I've done multiple tries with foreach loops without success. I must admit that I'm not completely familiar with the way they work.
This is what I've tried:
foreach ($school as $k => $v) {
if ($v['firstname'] == 'tom'){
echo 'Currently at the '.$k.'<br>';
}
}
This is the output is whish for:
Currently at the library
Currently at the canteen
$arr['canteen'][1]['lastname'] => jones