I was searching for a solution to this problem and saw that in an older version of PHP, using integers for this kind of addressing would result in an error. Of course, I can't find it now when I search.
However, I've found several references to unsetting nested arrays like this, and they look just like the one NogDog gave me and that I put into my app. I'm convinced that this is the correct syntax to be using.
I'm using PHP 5 on MAMP 1.2.1 on a ppc mac. running tiger. Could someone else see what happens when they run this? I'm wondering whether they get the same errors.
I'm going to post the code I'm using now below. This is mighty weird.
Thank you, whomever helps.
<?php session_start(); ?>
<html>
<?PHP
/* THIS IS A SHOPPING CART PROTOTYPE. THERE ARE THREE SCREENS. THE FIRST TWO ARE FORMS THAT GET SENT TO SELF PAGE.
FIRST PAGE GIVES FOUR BLANK TEXTBOXES, INTO WHICH THE USER INPUTS NUMBERS. THESE ARE STORED IN AN ARRAY IMAGINATIVELY CALLED 'A'.
ONCE STORED, THEY'RE PUT INTO ANOTHER ARRAY CALLED 'cart' AS VALUES OF AN ARRAY CALLED '$i'. (The final array will need to store more than two name=>value sets, that's why it's a multiple array.)
SECOND PAGE SHOWS WHAT HAS BEEN CHOSEN AND DRAWS A FORM WHERE THERE ARE CHECKBOXES TO REMOVE FROM THE CART.
SO SOMEONE HAS THE CHOICE TO REMOVE ITEMS FROM THEIR CART BEFORE PURCHASING
THIRD PAGE IS SUPPOSED TO ITERATE THROUGH THE TWO ARRAYS, AND IF AN ITEM HAS BEEN CHECKED, IT GETS UNSET FROM THE ARRAY.
THIRD PAGE ISN'T WORKING. */
/***************** DRAW 4 TEXTBOXES WHOSE CONTENTS WILL BE STORED *****************/
/***************** THIS IS THE FIRST SCREEN ANYONE SEES ***************************/
if ( (!ISSET ($_POST['A']) ) AND (!ISSET ($_POST['remove']) ) )
{
?>
<form method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
1 <input type="text" name="A[1]" size = "3"><br>
2 <input type="text" name="A[2]" size = "3"><br>
3 <input type="text" name="A[3]" size = "3"><br>
4 <input type="text" name="A[4]" size = "3"><br>
<input type="submit" value="test array">
<?PHP
}
/***************** THIS IS THE SECOND SCREEN ANYONE SEES *********************************************/
/***************** THIS DRAWS WHAT HAS BEEN CHOSEN AND GIVES CHECKBOXES FOR DELETION *****************/
if ( (ISSET ($_POST['A']) ) AND (!ISSET ($_POST['remove']) ) )
{
$cart = array();
$i = array();
$A = $_POST['A'];
foreach ($A as $x => $y)
{
echo ("x = $x and y = $y <br>");
if (!empty($y))
{
$i['id']=$x;
$i['qty']=$y;
array_push($cart, $i);
}
}
echo ("<br>");
?>
<form method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
<?php
foreach ($cart as $i)
{
echo ("id = $i[id] and qty = $i[qty] <input type=\"checkbox\" name=\"remove[]\" value=\"$i[id]\"><br>");
}
echo("<input type=\"submit\" value=\"remove checked\">");
$_SESSION['cart']=$cart;
}
/***************** THIS SHOWS THE ITERATION OF REMOVAL OF THE ARRAYS ******************/
/***************** THIS IS THE THIRD SCREEN ANYONE SEES ***************8888************/
if ((ISSET ($_POST['remove'])) AND (!ISSET ($_POST['A'])))
{
$remove = $_POST['remove'];
$cart = $_SESSION['cart'];
foreach ($remove as $id)
{
echo ("remove $id <br>");
foreach ($cart as $i)
{
if ($i["id"] == $id)
{
unset($cart[$i]['id']);
unset($cart[$i]['qty']);
// unset($cart[$i][id]);
// unset($cart[$i][qty]);
// unset ($i['id']);
// unset ($i['qty']);
// unset($cart[$i]['id']);
// unset($cart[$i]['qty']);
// unset($cart[$i['id']]);
// unset($cart[$i['qty']]);
// unset($cart[$i]);
}
echo ("id = $i[id] and qty = $i[qty]<br>");
}
}
echo ("<br><br>");
echo ("cart <br>");
foreach ($cart as $i)
{
echo ("id = $i[id] and qty = $i[qty] <br>");
}
}
?>
</html>