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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.