SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member Eradication's Avatar
    Join Date
    Nov 2004
    Location
    Virginia
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Deleting something from an Array

    Okay, well in my coding, I never use arrays, or not very often. When I do, I've never had to delete them.

    For a recent wishlist project, I decided to save the trouble of having tons of MySQL rows, and just create one in the user table 'wishlist' and have the array (imploded, of course) put there, and exploded, and pulled out on the page.

    The problem I've run across is removing things from your wishlist. I've never had to delete things from an array. So, pretty much I need help taking one value out of an array. For example, they may have itemids "1,2,3,4,5,6,7,8" in their wishlist, and I want to remove, say.. 4 from it.

    How would I go about doing that?

    Edit as far as Unset goes, I'm pretty sure to use it, you have to know where the value of the array is, to use it. I don't know where in the array (ie: $array[1]) the itemid is, so I don't think I can use it. Unless I could reorginize it, and then know its on $array[0] or something.

  2. #2
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    Southwest US
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not the most elegant solution but I usually do something like this:

    Code:
    $arrTemp = array();
    $intDelete = 4;
    for ($i=0;$i<count($arrItemids);$i++) {
         if ($arrItemids[$i] != $intDelete) {
              $arrTemp[] = $arrItemids;
         }
    }
    $arrItemids = $arrTemp;
    This code builds a temporary array to hold the values you want to keep, then assigns those values back to the $arrItemids array.

    If you need more explination, let me know.
    Thanks,
    Itshim

  3. #3
    SitePoint Wizard Lats's Avatar
    Join Date
    Jun 2003
    Location
    Melbourne, AU
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Eradication
    ...and I want to remove, say.. 4 from it.
    What about...
    PHP Code:
    unset($array[4]); 
    A small discussion about that in the manual though...

    http://au.php.net/array
    Lats...

  4. #4
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    Southwest US
    Posts
    203
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    With:
    Code:
    unset($array[4]);
    You will be removing the fifth element in the array, I believe Eradication wanted to remove the item id 4 from the array, not the element which corresponded to [4].

    Also by using unset() you are removing the element[4], but if there are elements [5], [6]... they will remain at [5], [6]... they will not collapse. This can cause a problem when using count(), because it will return the number of elements in the array. So if there are 7 elements, and you remove elements [2] and [3] then you call count() to use in a 'for loop', count() will return the integer 5 and you will never get to elements [6] and [7] in the loop.

    Hope this helps,
    Itshim


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •