Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    May 2006
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Deleting an array element by value

    I've been trying to figure out how to delete a part of an array by its value... for example, if I have an array called $numbers with the values 1, 2, 2, and 3 (with arbitrary keys), how would I go about deleting one of the '2's? I tried using the function:

    PHP Code:
    array_diff$numbers, array(['2']) 
    The problem is, this deletes ALL of the elements with the value '2' when I only want to delete one of them. Any ideas?

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,830
    Thanks
    21
    Thanked 157 Times in 148 Posts
    PHP Code:
    foreach ($numbers as $key => $val)
    {
          if (
    $val == 2)
          {
          unset(
    $numbers($key));
          break;
          }

    and you can reset the keys of your array with this (after you delete your array element):

    PHP Code:
    $numbers2 array_values($numbers); 
    Last edited by chump2877; 12-01-2006 at 10:21 AM.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :-)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!
    ♪♪ …Need Web Hosting For My YouTube-To-Mp3 Conversion Software? Check Here !!… ♪♪

  • #3
    New Coder
    Join Date
    May 2006
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Perfect. Thank you!


  •  

    Posting Permissions

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