0

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
  • foreach($_SESSION['cart'] as $item_key => $item_array) then $item_key will contain 2-1, 3-8, etc.. Commented May 24, 2010 at 11:43

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.