SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 1999
    Location
    On the road
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is probably a very stupid question, but I am totally stuck:
    I would like to delete an element from an array, not just give it a value of "" (nothing). Is that possible, and if yes, how?
    Thanks a lot!

    Johannes

  2. #2
    SitePoint Zealot oodie's Avatar
    Join Date
    Jul 2000
    Location
    Misty Mountain
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you know which element you want to delete, use array_splice. Example :
    Code:
    $a = array("A","B","C","D","E");
    
    // to remove 'D'
    array_splice($a,3,1);
    // now $a contain "A","B","C","E"
    (you can't use array_splice in PHP3)

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 1999
    Location
    On the road
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    (you can't use array_splice in PHP3)
    Well, that's a problem, my host doesn't offer php4 yet...
    Is there any known way with php3?

    Johannes

  4. #4
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check out the documentation in http://www.php.net about array
    I'm sure you could, but I'm sorry, I started with php4 right away, dunno how to do in php3 (maybe pop/shift?!)
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  5. #5
    SitePoint Zealot oodie's Avatar
    Join Date
    Jul 2000
    Location
    Misty Mountain
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    array_pop and array_shift also added in PHP4. If you know which element you want to remove I guess you can try this :

    Code:
    $k = 0;
    $flag = 0;
    for($i=0; $i<count($old_array); $i++) {
       /* loop for each element you want to remove
          (put them in $match array) */
       for($j=0; $j<count($match); $j++) {
          if($old_array[$i] == $match[$j]) {
             $flag = 1;  /* if match, set flag to 1 */
             break;
          }
       }
       /* keep it if it's not the one to remove */
       if($flag == 0) 
          $new_array[] = $old_array[$i];
    }
    It should work.



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
  •