Jump to content

Recommended Posts

Hi all, I can't seem to work out how to get the index number from a session array. Could someone quickly show me how to do this.

I basically want to create a series of 'delete' links for a shopping cart. Each link should delete the specific item from the session array, but I can't seem to identify the unique index number in the array when the foreach loop runs:

 

My current code:

 

<?php
// display the delete links for each item in the basket
foreach ($_SESSION['cart']['item'] as $item_del){
	// show the remove button and set it to remove this item
	echo "<a href='cart.php?action=delete&cat_id=" . $cat_id . "&del_id=" . $item_del . "' target='_self'>delete</a><br />";
// used to see the array index number - not working at the moment
	echo $item_del;
}	
?>

Link to comment
https://forums.phpfreaks.com/topic/123904-get-the-index-number-in-an-array/
Share on other sites

Hi all, I can't seem to work out how to get the index number from a session array. Could someone quickly show me how to do this.

I basically want to create a series of 'delete' links for a shopping cart. Each link should delete the specific item from the session array, but I can't seem to identify the unique index number in the array when the foreach loop runs:

 

My current code:

 

<?php
// display the delete links for each item in the basket
foreach ($_SESSION['cart']['item'] as $item_del){
	// show the remove button and set it to remove this item
	echo "<a href='cart.php?action=delete&cat_id=" . $cat_id . "&del_id=" . $item_del . "' target='_self'>delete</a><br />";
// used to see the array index number - not working at the moment
	echo $item_del;
}	
?>

Foreach can return the key as well as the value, us the following syntax

foeach($array as $key => $value)

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.