Help - Search - Members - Calendar
Full Version: trouble with array manipulation (PHP)
HTMLHelp Forums > Programming > Server-side Scripting
Technetium
I have an array in which I need to be able to delete any number of entries from, and have the array move everything above the deleted entries down so that there are no spaces. What I am using for code right now:
CODE

<?php
  if(!empty($_POST["delete"]))
  {
    for($i=0;$i<$_SESSION["items_in_cart"];$i++)
    {
      if($_POST["deleteme".$i]==$i)
      {
        for($j=$i;$j<$_SESSION["items_in_cart"]-1;$j++)
        {
          $_SESSION["item"][$j]["ID"]=$_SESSION["item"][$j+1]["ID"];
          $_SESSION["item"][$j]["size"]=$_SESSION["item"][$j+1]["size"];
          $_SESSION["item"][$j]["number"]=$_SESSION["item"][$j+1]["number"];
        }
        $_SESSION["items_in_cart"]--;
      }
    }
  }
?>

I've been testing it with 3 items in the array. It works correctly if I delete the first item. If I delete the second item, however, it actually deletes the first and third, leaving only the second. And if I delete the third item, it actually deletes the second item. I haven't even tried deleting more than one at a time yet since I'm having enough problems with just one at a time.

I can't figure out why it is doing this. I have been very careful not to mix up $i and $j. I'm pretty sure that the second for-loop's terminating condition has to be one less than the first, because it's going to set something equal to $j+1 and it would otherwise read out of the array bounds.

EDIT: Just for clarification... $_POST["delete"] refers to the name of the submit button that deletes items checked. $_POST["deleteme".$i] refers to the checkboxes which are created by a for-loop and assigned names like deleteme0, deleteme1, etc, and values are simply 0, 1, etc.
Technetium
Also, after making this thread I found out that PHP has a built-in way of doing this, but I can't get that to work either. I tried using this:
CODE

<?php
  if(!empty($_POST["delete"]))
  {
    $max=$_SESSION["items_in_cart"];
    for($i=0;$i<$max;$i++)
    {
      if($_POST["deleteme".$i]==$i)
      {
        unset($_SESSION["item"][$i]["ID"];
        unset($_SESSION["item"][$i]["size"];
        unset($_SESSION["item"][$i]["number"];
        $_SESSION["items_in_cart"]--;
      }
    }
    $_SESSION["item"]=array_values($_SESSION["item"]);
  }
?>

Using this, it correctly reduces the number of items, but it leads to the first element in the array being uninitialized (I know this because it the "ID" refers to an image, which now appears in the page as a broken image, and the other two pieces of data are numbers that are displayed on the page, and those come up as 0.).
Ephraim F. Moya
You're scanning the whole array every time. Don't do that.

Change _POST['deletemeX'] to _POST['idToDelete'] then just go right there.

unset $_SESSION[...][$_POST['idToDelete']]...;
etc.

It looks like you have two keys. 'item' and 'id'. You only need one.

What a mess!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2014 Invision Power Services, Inc.