Jump to content

Unset from array not working


blmg2009

Recommended Posts

The "5" in your unset() example needs to refer to a key...assuming that $list is your array. You could use a foreach loop to get the array index for the items you want to remove and then use unset().

 

Note that you could also consider using array_flip() to swap the array's keys and values; unset the items you want to remove; and then flip the array back. More information about array_flip() can be found here:

http://php.net/manual/en/function.array-flip.php

 

Of course, you'll want to make sure there are no duplicate values in the array before using array_flip().

  • Like 1
Link to comment
Share on other sites

You can also use array_search() to look for a value in the array and if it exists it will return the associated key (or FALSE if not). You can then use the key to unset the index from the array.

if (($key = array_search($value_to_delete, $your_array)) !== false) {
  unset($your_array[$key]);
}
Edited by CroNiX
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.