That code is actually very close to what you need. What you need to do
is to put the store's name, which is the index of the first level.  As
it is, the first foreach is completely ignoring the indexes of the first
level.

$store is ends up being the array for the second foreach, but to the
first foreach it's nothing but the "value" of the first level array.
Basically, you need to setup the first foreach to look much like the
second foreach.

foreach will only handle one dimension at a time. Which is why I find it
easier to term each dimension as really a "level" in multi-dimensional
arrays.

Sorry for being vague; I could outright show what the first two lines
need to be to get what you want, but this looks like a homework
question. And even if it's not, you're so close with what you have that
I'd think it be better to try to nudge you in the right direction.

-Nick

On Sun, 2011-07-03 at 14:52 -0500, Byron Como wrote:
> The format I've been experimenting with is:
> 
> 1. foreach ($foodprices as $store){
> 2. // echo $store .'<br>';
> 3.
> 4. foreach ($store as $item=>$price){
> 5. echo 'Item: '. $item . ' at '. $price . '<br>';
> 6. }
> 7. }
> 
> Which prints:
> Item: pizza at $11.95
> Item: spinach at .75c
> Item: tomatos at 3 for a dollar
> Item: anchovies at $2.25
> Item: pizza at $15.99
> Item: spinach at $2.25
> Item: tomatos at $1.95 / pound
> Item: anchovies at $8.75
> 
> Uncommenting line 2 results in the word 'array' being printed, not the 
> store name.
> 
> How many dimensions can this foreach construct handle?. Is there another 
> data structure I could use here? I can only make it print the last two 
> elements OR the first element , but not all three.
> 
> On 7/3/2011 2:18 PM, Nicholas Istre wrote:
> > foreach ($array as $index =>  $value) {}
> >
> > You have a two-dimentional array there.  There's two levels of arrays,
> > and you're going to need foreach twice, once for each level.  The first
> > level with have the store name as $index above and the store's price
> > list array as the $value. The second level foreach will take the store's
> > price list array and use the item as the $index and the price as the
> > $value.
> >
> > Why don't you show the printout code you currently have? I have a
> > suspicion of what you're doing, but it's hard to tell without the code
> > (or a subset, basically I need to see how you have your foreachs setup.)
> >
> > -Nick
> >
> > On Sun, 2011-07-03 at 14:01 -0500, Byron Como wrote:
> >> <?php
> >>
> >>
> >> $foodprices =array('Albertsons'=>
> >>
> >>    array('pizza'=>'$11.95','spinach'=>'.75c','tomatos'=>'3 for a
> >> dollar','anchovies'=>'$2.25'),
> >>                               'Whole Foods'=>
> >>
> >>    array('pizza'=>'$15.99','spinach'=>'$2.25','tomatos'=>'$1.95 /
> >> pound','anchovies'=>'$8.75'));
> >>
> >>
> >> foreach......
> >>
> >>
> >> ?>
> >>
> >> Desired output:
> >>
> >> Albertsons
> >> pizza $11.95
> >> spinach .75c
> >> tomatos 3 for a dollar
> >> anchovies $2.25
> >> Whole Foods
> >> pizza $15.99
> >> spinach $2.25
> >> tomatos $1.95 / pound
> >> anchovies $8.75
> >>
> >> The problem: I can't get the store name to print out, just the item and
> >> the price. The store names appear as 'array'.
> >>
> >> _______________________________________________
> >> General mailing list
> >> [email protected]
> >> http://brlug.net/mailman/listinfo/general_brlug.net
> >
> >
> > _______________________________________________
> > General mailing list
> > [email protected]
> > http://brlug.net/mailman/listinfo/general_brlug.net
> >
> 
> 
> _______________________________________________
> General mailing list
> [email protected]
> http://brlug.net/mailman/listinfo/general_brlug.net



_______________________________________________
General mailing list
[email protected]
http://brlug.net/mailman/listinfo/general_brlug.net

Reply via email to