toyfruit Posted September 12, 2008 Share Posted September 12, 2008 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 More sharing options...
priti Posted September 12, 2008 Share Posted September 12, 2008 Hi, Check the associative array. echo '<pre>'; print_r($_SESSION); and correct the order either print your session array so one can suggest you the correct way to do the same. Thanks Link to comment https://forums.phpfreaks.com/topic/123904-get-the-index-number-in-an-array/#findComment-639636 Share on other sites More sharing options...
wildteen88 Posted September 12, 2008 Share Posted September 12, 2008 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) Link to comment https://forums.phpfreaks.com/topic/123904-get-the-index-number-in-an-array/#findComment-639940 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.