Get the current array key in a multi dimensional array

Posted by johlton on Stack Overflow See other posts from Stack Overflow or by johlton
Published on 2010-05-24T11:38:40Z Indexed on 2010/05/24 11:41 UTC
Read the original article Hit count: 310

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

© Stack Overflow or respective owner

Related posts about php

Related posts about session