Re: Re: [PHP-DEV] removing an item from an array
| From: | Tyler L | Date: | Thu, 16 Aug 2012 04:29:11 +0000 |
| Subject: | Re: Re: [PHP-DEV] removing an item from an array | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-62210@lists.php.net to get a copy of this message | ||
On Wed, Aug 15, 2012 at 7:59 PM, Morgan L. Owens <packrat@nznet.gen.nz>wrote:
> On 2012-08-16 08:27, Nikita Popov wrote:
>
>> On Wed, Aug 15, 2012 at 10:22 PM, Stas Malyshev <smalyshev@sugarcrm.com>
>> wrote:
>>
>>> Hi!
>>>
>>> How come there is no straight-foward obvious way to simply remove a
>>>> given
>>>> value from an array?
>>>> 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.
>>>
>>
>> Btw, deleting all values (not just the first) is also very easy currently:
>>
>> foreach (array_keys($array, $delValue) as $key) {
>> unset($array[$key]);
>> }
>>
>> $array = array_diff($array, [$delValue]);
>
>
>
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
http://php.net/array_flip
This is my favourite way of removing a value:
$kv = array( 1 => 'a', 2 => 'b', 3 => 'c');
$vk = array_flip($kv);
unset($vk['b']);
$kv = array_flip($vk);