how to choose a "sub-array" in a foreach loop
arrays, foreach, php
Solution
There's no need for any looping, although you should implement some kind of error checking to ensure that the name exists in the persons array.
function findHotty($name) {
if (!isset($person[$name])) {
// do something when the name doesn't exist
}
return $person[$name];
}
Problem
I have an array of arrays ``` $person = array(2) { [billy]=> array(3) { ["height"]=> string(60) "tall" ["build"]=> string(7) "slim" ["attractiveness"]=> string(7) "extremely" } [carl]=> array(3) { ["height"]=> string(60) "short" ["build"]=> string(7) "chubby" ["attractiveness"]=> string(7) "neglegible" } ``` Unfortunately for Carl, there just isn't any interest in him. Billy conversely is extremely in demand, and people would like to see Billy's details out and about. So I want to loop through the array and return only Billy's information. ``` function findHotty($billy){ foreach ( $person as $meat) { // wizardry return onlyBilly } } ``` I was hoping to pass the name of the subarray in the function, so if tomorrow Carl is all the rage - he has a certain undeniable charm - when I call the method I can pass it Carl and it looks for his sub-array. I hope that's clear!