1

I have an array with several elements. The array keys are numeric.

I now want to remove a certain element. All I know is the element's content, not the key.

Is there an easy way to remove this element from the array? Or do I need to loop through all elements of the array to get the key and then unset the element with that key?

asked Nov 28, 2010 at 11:45
1
  • for the several elements it doesn't really matter. Commented Nov 28, 2010 at 11:50

4 Answers 4

3

You can use array_filter to remove all elements with that particular value:

$arr = array_filter($arr, function($val) { return $val !== 'some value'; });

This example uses an anonymous function that were introduced with PHP 5.3. But you can use any other callback function.

Or you can use array_keys to get the keys of all elements with that value and do a diff on the keys after:

$arr = array_diff_key($arr, array_flip(array_keys($arr, 'some value', true)));
answered Nov 28, 2010 at 11:52

6 Comments

+1: didn't know the array_filter function, seems to be really handy.
@nico array_filter seems overkill if you have unique elements in your array. it will loop through all elements. And array_flip will spoil your data if there are non-unique elements. Both solutions are funny, but impractical. Being not an actor, but a programmer, always make your code as easy and readable as possible. You'll lave to read it in the future. So, your own one is way better.
@Col. Shrapnel: The complexity of both array_search and array_filter is O(n). And array_flip is only applied of an array of keys; so there won’t be any information loss.
@Gumbo, Dont worry, here is another +1, for a great answer.
@Col. Shrapnel: Yes, the average behavior is O(n/2) for array_search versus Θ(n) for array_filter; but it’s still O(n) in both cases.
|
1

You can use the array search function to get the key

$key = array_search('searchterm', $items); 
unset($items[$key]);
answered Nov 28, 2010 at 11:49

1 Comment

Just be sure to check the return value for array_search (see my answer below)
0

sure

foreach($myarray as $key=>$item) {
 if($item=='myknowncontent') { unset($myarray[$key]; }
}
answered Nov 28, 2010 at 11:47

7 Comments

Well, yes, that's what I mentioned. I was just wondering if I could unset the element without looping through all elements of the array.
As Col. Shrapnel said for the several elements it doesn't really matter
@Starx. Wow you got me, I am not a Native English Speaker. Then... What..(Your Point)
Nothing, just did not understand the meaning of the sentence you quoted, that's all! :)
@nico you have to loop through all elements only if they aren't unique. otherwise you can use break operator.
|
0

You can use array_search to find the corresponding key:

$key = array_search("whatever", $array);
if ($key !== FALSE)
 unset($array[$key]);
answered Nov 28, 2010 at 11:49

1 Comment

This will remove $array[0] if there is no element with such value.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.