This is linked from a previous thread, but it seems to be a different problem now. The referencing errors are gone now, but I'm having loop trouble.
Much thanks to bpat1434!
Old thread, in case anyone's interested:
http://www.phpbuilder.com/board/showthread.php?t=10333856 - 'Unsetting arrays in arrays'
How this is set up:
I have a multidimensional array $cart:
$cart =
Array (
[0] => Array (
[id] => 1
[qty] => 9 )
[1] => Array (
[id] => 2
[qty] => 8 )
[2] => Array (
[id] => 3
[qty] => 7 )
[3] => Array (
[id] => 4
[qty] => 6 ) )
I have another array $remove, which is dynamically created from checkboxes that correspond to the [id] values in the $cart array: 1,2,3 & 4, or any combination of them. The user clicks on a checkbox to remove an item from the cart.
Let's say that $remove contains 2,3,4.
I have three nested foreach() loops:
foreach ($remove as $rem)
foreach ($cart as $i)
foreach($i as $key => $value)
So that for each one that a user chooses to remove, it ambles through the $cart array looking for [id]'s that match it. If it does, it's supposed to unset that array from $cart.
One of the problems that I found was that I couldn't get it to reference $i (in the second and third loop). Then when I asked it to echo $i, it spits out 'Array'. So $i isn't pointing to the index number in brackets, it's pointing at something else that comes out as a string when I ask it to print.
So I came up with a workaround. I created a counter so that it initializes to zero and when the loops match an ID that wants to be removed, then I substitute the counter for the number of the [id] that needs to be banished. I'm not pleased that I needed to do this, but I couldn't think of a more elegant solution. I've been at this for days now and just want it done.
This removed all of the errors I was getting.
The problem I'm having now:
It only removes the first, or first and second elements that need to go. It never removes the last element.
After running the process on 2,3,4 as the [id]s to be terminated, here's the resulting $cart array: (1 & 4 remain.)
$cart
Array (
[0] => Array (
[id] => 1
[qty] => 9 )
[3] => Array (
[id] => 4
[qty] => 6 ) )
with 1,2,3 as the [id]s to be terminated: (3,4 remain)
$cart
Array (
[2] => Array (
[id] => 3
[qty] => 7 )
[3] => Array (
[id] => 4
[qty] => 6 ) )
with 1,4 as the [id]s to be terminated: (2,4 remain) HUH?
$cart
Array (
[1] => Array (
[id] => 2
[qty] => 8 )
[3] => Array (
[id] => 4
[qty] => 6 ) )
Okay, here's the code for that section:
Without the tests:
$remove = $_POST['remove'];
$cart = $_SESSION['cart'];
$count = count($cart);
foreach ($remove as $rem)
{
$counter=0;
foreach ($cart as $i)
{
foreach($i as $key => $value)
{
if (($key =='id') AND ($value == $rem))
{
unset($cart[$counter]);
}
}
$counter++;
}
}
With the tests:
(I include this just in case someone can give me pointers on how to design tests. I'm really new at this.)
$remove = $_POST['remove'];
$cart = $_SESSION['cart'];
$count = count($cart);
echo("<br><br> Cart <br>");
print_r($cart);
echo("<br><br>");
foreach ($remove as $rem)
{
echo ("<br><br>remove $rem <br><br>");
$counter=0;
foreach ($cart as $i)
{ echo ("$i <br>");
foreach($i as $key => $value)
{
echo("index = $counter <br>");
echo ("$i <br> key = $key and value = $value <br>");
if (($key =='id') AND ($value == $rem))
{
unset($cart[$counter]);
}
}
$counter++;
echo("Cart <br>");
print_r($cart);
echo("<br><br><br>");
}
}
echo("<br> Final Cart <br>");
print_r($cart);