Like this?
foreach($food as $produce) {
foreach($produce as $name => $value) {
echo "The produce $name has value: $value\n";
}
}
The first for loop just loops through the first array, it doesn't have significant keys we need, so I just get the reference to the value and store it in the value $produce
Then, we loop through the array that is $produce
, but this time key and value are both significant.
That's why we use $name => $value
in this loop, so we get both values we need.
Some prefer to always use $key => $value
, but I prefer to give variables the name of the value they represent.
Now if you need a specific fruit, you could wrap it in a function to search for it
/**
* Returns the fruit name from supplied food array.
* @var $food array[array[string => value]]
* @var $fruitName string The name of the fruit you want
* @returns
**/
function findFruit($food,$fruitName) {
foreach($food as $produce) {
foreach($produce as $name => $value) {
if($name == $fruitName) {
return $value;
}
}
}
}
$quantityOfBananas = findFruit($food, "Banana");//5