I am using a form to create several arrays in a Session. Each time the form is submitted a new $_SESSION[‘item’][] is made containing each new array. The code for this:
$newitem = array (
'id' => $row_getshoppingcart['id'] ,
'icon' => $row_getimages['icon'],
'title' => $row_getimages['title'],
'medium' => $row_getshoppingcart['medium'],
'size' => $row_getshoppingcart['size'],
'price' => $row_getshoppingcart['price'],
'shipping' => $row_getshoppingcart['shipping']);
$_SESSION['item'][] = $newitem;
There could be any number of item arrays based on how many times the user submits the form. Any ideas how can I get the value of the array key that is being put in place of the [] in the session variable? I am trying to create a remove from cart option and cannot figure out how to reference that particular array in the session to unset it.
I am currently displaying the items as such:
<?php foreach ( $_SESSION['item'] AS $item )
echo $item['title'];
echo $item['icon'];
and so on…
Thank you in advance for your time. I really appreciate it.
foreach($_SESSION['item'] as $key => $value)will enable you to seperate the key and value, and ofcourse, to access the value current key has.To extend this with an example, consider following code:
will output:
The key foo has the value of bar.The key foo2 has the value of bar2.However, in the same way, if the $arrValue variable is known to hold an array, it will keep it’s content. To loop through that second array, you will need to loop it through another foreach statement.