I have an array with 5 columns and 4 rows. I want to delete one of the rows based on the variable "del" that I pass. If the variable "del" matches the first column of any of the 4 rows, I want to delete that row from the array. I have been trying to use the "unset" function, and can't seem to remove the row. Below is my code:

#delete row from cart array if delete link pressed and variable passed

if ($_GET['del'] <>"")
{
echo "delete value is:".$_GET['del'];
$arr=$_SESSION['cart'];

foreach ($arr as $rownum=>$value)
{
echo "<br>Row values are:".$arr[$rownum][0];
if ($arr[$rownum][0]==$_GET['del'])
	{
	echo "it's a match";
	unset($arr[$rownum]);
	}
}
}

The "del" variable is being passed correctly. It matches the specified column in the array, but it seems that

unset($arr[$rownum]);

is not working to delete that row from the array. Can anyone provide me with some guidance?

    Yes, thank you for the link. I have been to the php website and I found the unset function as well as the splice function for trying to remove a row from an array, however, I have not been successful as you can see from my code.

    I was hoping someone knew of a way to do this.

      array_splice must be the one function which you need I think

        Are you trying to unset from the session or the copy of the session? By saying $arr=$SESSION['cart'];, you've just made a copy of that array in the $arr array. So doing an unset on $arr should work fine, but it would not however unset from $SESSION['cart'].

          LordShryku- hey man you hit the nail on the head. I was copying the array from session, assigning it to $arr, deleting the row, and then forgot to assign $arr back to the session.

          Once I did that, everything was kosher. Thanks for your help!

            Write a Reply...