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
)
)
)
print_ryour$_SESSIONarray and add it your question? I bet just doing that might answer your question.