Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How do I remove an element from an array when I know the elements name? for example:

I have an array:

$array = (apple, orange, strawberry, blueberry, kiwi);

the user enters strawberry

strawberry is removed.

To fully explain:

I have a database that stores a list of items separated by a comma. The code pulls in the list based on a user choice where that choice is located. So, if they choose strawberry they code pulls in every entry were strawberry is located then converts that to an array using split(). I want to them remove the user chosen items, for this example strawberry, from the array.

share|improve this question
Same question: stackoverflow.com/questions/7225070/… – trante Jan 10 at 19:42

7 Answers

up vote 21 down vote accepted

Use array_search to get the key and remove it with unset if found:

if (($key = array_search('strawberry', $array)) !== false) {
    unset($array[$key]);
}

array_search returns false (null until PHP 4.2.0) if no item has been found.

And if there can be multiple items with the same value, you can use array_keys to get the keys to all items:

foreach (array_keys($array, 'strawberry') as $key) {
    unset($array[$key]);
}
share|improve this answer
Getting an odd result. I used your first suggestion since there will always only be one instance. To test it I simply had it output the key value. Worked. However, it wont unset. – dcp3450 Mar 15 '10 at 17:50
@dcp3450: Are you sure? It works like charms for me. – Gumbo Mar 15 '10 at 17:55
while(odbc_fetch_row($runqueryGetSubmenus)) { $submenuList = odbc_result($runqueryGetSubmenus,"submenus"); $submenuArray = split(',',$submenuList); if (($key = array_search($name,$submenuArray)) !== false) { unset($submenuArray[$key]); } } – dcp3450 Mar 15 '10 at 18:02
@dcp3450: And what do you do with $submenuArray? (Note that with each loop $submenuArray will be overwritten.) – Gumbo Mar 15 '10 at 18:09
i updated my question to better explain. Basically the code loops through entries in a database removing the chosen items, "strawberry" in this example. So, the user enters a selection => the code searches under submenus and finds any list that has that option => turns that list into an array => removes the chosen option. – dcp3450 Mar 15 '10 at 18:13
show 2 more comments
if (in_array('strawberry', $array)) 
{
    unset($array[array_search('strawberry')]);
}
share|improve this answer
2  
You should test if strawberry is in the array at all. – Gumbo Mar 15 '10 at 17:10
Following Gumbo's advice modified answer to include verification of the element before removing it from the array. – John Conde Mar 15 '10 at 17:18
1  
Also keep in mind that the indices aren't realigned after deleting a particular element. In other words, the index sequence will have gaps then. If you delete 'strawberry' from your example, 'kiwi' will still have index 4, and index 2 will simply disappear. It matters if your code relies on the completeness of the index sequence, as for example for($i = 0; $i <.., $i++) loops do. – the-banana-king Mar 15 '10 at 17:32
To add to what the-banana-king said, if you want to reindex the array, simply do $my_array = array_values($my_array);. – ryeguy Mar 15 '10 at 17:41
1  
While this solution is correct, it searches the array TWICE (in_array and array_search). Using the return of array_search as in Gumbo's answer is more effective – Bogdan D Mar 19 at 11:41

A better approach would maybe be to keep your values as keys in an associative array, and then call array_keys() on it when you want to actual array. That way you don't need to use array_search to find your element.

share|improve this answer

Will be like this:

 function rmv_val($var)
 {
     return(!($var == 'strawberry'));
 }

 $array = array('apple', 'orange', 'strawberry', 'blueberry', 'kiwi');

 $array_res = array_filter($array, "rmv_val");
share|improve this answer

Using array_seach(), try the following:

if(($key = array_search($del_val, $messages)) !== false) {
    unset($messages[$key]);
}

array_search() returns the key of the element it finds, which can be used to remove that element from the original array using unset(). It will return FALSE on failure, however it can return a "falsey" value on success (your key may be 0 for example), which is why the strict comparison !== operator is used.

The if() statement will check whether array_search() returned a value, and will only perform an action if it did.

share|improve this answer

$arr = \array_filter($arr, function ($v) { return $v != 'some_value'; }

share|improve this answer
unset($array[array_search('strawberry', $array)]);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.