Jump to content

Get the index number in an array


toyfruit

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)

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.