0

I have a PHP shopping cart in the $_SESSION array and I am trying to display it's content.
Here is my relevant code (i have a session_start at the beginning of my page):

if (isset($_GET['item']))
    {
        $item = $_GET['item'];

        if(!isset($_SESSION['cart'])) 
        {
            $_SESSION['cart'] = array();
        }

        if (isset($_SESSION['cart'][$item]))
        {

            $_SESSION['cart'][$item]['quantity']++; 

            echo "<h3 class=changement> Additional Item ".$item." has been added to cart.</h3>";
        }
        else
        {   
            $_SESSION ['cart'][$item]['quantity']= 1;

            echo "<h3 class=changement> Item ".$item." has been added to cart.</h3>";
        }

       foreach($_SESSION['cart'][$item] as $myItem => $myQuantity)
       {
           echo "<br> item: ".$myItem." quantity: ".$myQuantity."<br>";
       } 

The foreach to display the contents of the cart does not display the name of the item.
The contents of the $myItem is always: quantity. Like so:

item: quantity quantity: 10

I would like the foreach to print:

item: Banana quantity: 4
item: Apple  quantity: 6

Obviously, there is something that I'm not getting about multi-dimensional arrays.
Can someone clear this up.

Note: I am aware that instead of using this line : $_SESSION['cart'][$item]['quantity']++; I could use just $_SESSION['cart'][$item]++; and have the item name as a integer variable to increment but I'm trying to understand how multi-dimensionnal arrays work with php.

Array ( 
   [connectee] => rush, 
   [start] => 1419894221, 
   [expire] => 1419894281,
   [cart] => Array ( 
       [troutster] => Array ( 
           [quantity] => 2 
       ) 
       [funny_man] => Array ( 
           [quantity] => 1 
       ) 
       [Gareth] => Array ( 
           [quantity] => 1 
       ) 
   ) 
)
2
  • Can you print_r your $_SESSION array and add it your question? I bet just doing that might answer your question. Commented Dec 29, 2014 at 22:58
  • @dan08 Contents of $_SESSION with print_r: 'Array ( [connectee] => rush [start] => 1419894221 [expire] => 1419894281 [cart] => Array ( [troutster] => Array ( [quantity] => 2 ) [funny_man] => Array ( [quantity] => 1 ) [Gareth] => Array ( [quantity] => 1 ) ) )' Commented Dec 29, 2014 at 23:06

2 Answers 2

2
foreach($_SESSION['cart'] as $itemName => $item) {
    echo "<br> item: ".$myItem." quantity: ".$item["quantity"]."<br>";
}

Because $item is an object, and $_SESSION['cart'] is the array that you want to show

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

Comments

0

This worked:

foreach($_SESSION['cart'] as $myItem => $myQuantity)
       {           
           foreach($myQuantity as $value)
           {
               echo "<br> item: ".$myItem." quantity: ".$value."<br>";
           }
       } 

$myQuantity was an array, so I had to access it as such.

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.