On Wed, Aug 15, 2012 at 10:22 PM, Stas Malyshev <smalys...@sugarcrm.com> wrote:
> Hi!
>
>> How come there is no straight-foward obvious way to simply remove a given
>> value from an array?
>
> The same reason there's no simple way to undefine variable whose value
> is 42 without knowing the variable name. Array is a container indexed by
> keys, not values. So if you've got just a value, there's no way to know
> if it's in the container at all, and if it is, where it is, except for
> going through all the values and checking if any of them is equal to
> what you nedd.
>
>> Just look at the number of horrible ways people solve this obvious problem:
>
> I see:
> if(($key = array_search($del_val, $messages)) !== false) {
>     unset($messages[$key]);
> }
>
> Nothing horrible here.

In addition to that, one should be aware that a value can exist
multiple times in an array, whereas keys are unique. So there are
infinitely many possible deletion strategies.

Btw, deleting all values (not just the first) is also very easy currently:

foreach (array_keys($array, $delValue) as $key) {
    unset($array[$key]);
}

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to