Jump to content

multidimensional array foreach loop


vipsa

Recommended Posts

I am a complete beginner so I need help.

 

I have the following code:

 

<code>

 

$foodprices = array("vegetables"=>array("Tomatoes"=>1.00, "Beet"=>20.5, "Onions"=>3.5),

"Meat"=>array("Skaap"=>20.5, "Bees"=>30.5));

var_dump($foodprices);

 

foreach($foodprices as $category)

{

echo "The current category is $category";//print the category

foreach($category as $food => $price)

{

echo "<br>The price of {$food} is {$price}";//print each item and its price

}

}

</code>

 

I would like to print the category names as well so how do I print the key values of a multidimensional array please. Any help is appreciated

Link to comment
https://forums.phpfreaks.com/topic/272843-multidimensional-array-foreach-loop/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.