tags:

views:

241

answers:

4

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.

+7  A: 
if (in_array('strawberry', $array)) 
{
    unset($array[array_search('strawberry')]);
}
John Conde
You should test if *strawberry* is in the array at all.
Gumbo
Following Gumbo's advice modified answer to include verification of the element before removing it from the array.
John Conde
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.
To add to what the-banana-king said, if you want to reindex the array, simply do `$my_array = array_values($my_array);`.
ryeguy
A: 

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.

jishi
+1  A: 

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]);
}
Gumbo
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
@dcp3450: Are you sure? It works like charms for me.
Gumbo
while(odbc_fetch_row($runqueryGetSubmenus)) { $submenuList = odbc_result($runqueryGetSubmenus,"submenus"); $submenuArray = split(',',$submenuList); if (($key = array_search($name,$submenuArray)) !== false) { unset($submenuArray[$key]); } }
dcp3450
@dcp3450: And what do you do with `$submenuArray`? (Note that with each loop `$submenuArray` will be overwritten.)
Gumbo
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
I got it! I wasn't rewriting the new array back to the DB. Easy to miss stuff when you stare at code for so long.
dcp3450
@dcp3450 Agree with @Gumbo. In the sample code you posted above, You aren't doing anything with $submenuArray after removing the user selected option and that variable is constantly reused in the loop. You need to save it to the database or use a multidimensional array to access your modified $submenuArray later.
ghoppe
A: 

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");
D.Martin