foreach loop through a multidimensional array

I am a complete beginner so I need help.

I have the following code:

[php]
$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”;
foreach($category as $food => $price)
{
echo “
The price of {$food} is {$price}”;
}
}
[/php]

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

print_r works better than echo for array loops in my experience…it will also show you all possible variable keys.

You can do something like echo $category[‘key_value’] to echo the specific variable you are interested in. In the case of your example, echo $category[‘vegetables’] or whichever key you’re after.

There are two categories and under each category there are a number of items so I would like to print

Category 1 (Vegetables)
Carrot => 2
Beet => 3

Category 2 ("meat)
Lamb => 3.0
Beef => 20.5

How do I do that please?

Okay so…here is a trick I use when I’m trying to fetch a value or key from an array inside another array.

[$array][‘array_key’][$sub_array][‘sub_array_key’] --> and so forth indefinitely stacking arrays and array keys.

In your case, lets say you have an array for food categories that is comprised of other arrays.

$food_category = array(‘vegetables’ => $veg_array, ‘meat’ => $meat_array);

You would want to echo something like

echo [$food_category][‘vegetables’][$veg_array][‘carrot’];

to get the value for the Carrot key from inside the two two arrays.

print_r([$food_category][‘vegetables’][$veg_array]); //this would show you all keys and values for the $veg_array

If I have 1000 categories I want to transverse through each category and display the category and the items under each category so I don’t see your method working sorry.

…Use the foreach loop you had in your first post with the suggestions I gave. -__-

Go here for more help:
http://php.net/manual/en/language.oop5.iterations.php

Your loop appears to already do what you want? Aside from $category being an array.

[php]
foreach($foodprices as $name => $category) {
echo “The current category is {$name}”;
foreach($category as $food => $price) {
echo “
The price of {$food} is {$price}”;
}
}
[/php]

I want to display the category name of each category and then the products below that. The loop must walk through the code and display the category name then all the products in that category and then the next category and so on. Can somebody give me an example of the code I should use. How do I access the first dimension of an array? using a loop is what I’m after. Please any help will be appreciated

I feel like I just gave you an example of what you want. Can you use my code and show the output, then show the output you would like??

Hi M@tt

Sorry I did not see the code you gave me and it works great Thank you so much!

Sponsor our Newsletter | Privacy Policy | Terms of Service