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

Reply via email to