views:

106

answers:

2

Hi *,

I have a session array *$_SESSION['cart']* with some items in it. The structure ist like this (via *print_r*):

    Array (
      [2-1] => Array (
         [color] => 7
         [articlenumber] => WRG70 10
         [quantity] => 1
         [price] => 17.50
      )

      [3-8] => Array (
         [color] => 2
         [articlenumber] => QRG50 02
         [quantity] => 1
         [price] => 13.50
      )
   )

Looping over the values for display is fine ...

foreach($_SESSION['cart'] as $item_array)
{ 
   foreach($item_array as $item => $value)
   {   
      echo $value . ' | ';
   }
}

... since it results in something like this:

7 | WRG70 10 | 1 | 17.50 |
2 | QRG50 02 | 1 | 13.50 |

But Now: How can I output the matching key (e.g. '2-1') as well? I tried some array functions like key() & current but couldn't get it to work (one of these days).

Any quick hint on this?

Thanks a lot and best from Berlin

Fabian

+1  A: 

foreach ($array as $key => $value) {...}

I see you're already using this in the inner foreach loop, add it to the outer one as well, and you'll have access to the key.

Maerlyn
Wow. Thanks a lot. :)
johlton
+1  A: 

Try this:

foreach($_SESSION['cart'] as $key => $item_array)
{ 
   foreach($item_array as $item => $value)
   {   
      echo 'Key = ' . $key . ' Value = ' . $value . ' | ';
   }
}
Sarfraz
Wow. That helped immediately. Thanks a lot. :)
johlton
@johlton: You are welcome......
Sarfraz