Morgan L. Owens | 18 Aug 2012 15:52
Picon
Favicon

[PHP-DEV] re: removing an item from an array

Rasmus Schultz wrote:

 > I disagree - this is (or should be) a simple, atomic operation...
 > yet, you've got a function-call, an intermediary variable, a boolean
 > test, and an unset statement repeating the name of the array you're
 > deleting from.
 >
 > This should be a simple statement or function/method-call, and in
 > most other languages it would be...

How simple is it? Does it:

1) Remove one occurrence of the element (presumably the first) or all?
2) Reindex the array (as someone else argued was necessary to make it 
"properly indexed" afterwards) or not?
3) Modify the array in-place or return a modified array?
4) Use type-strict or normal comparisons?

That's sixteen different interpretations of "remove a given value from 
an array" right there. Leave support for any of them out and someone is 
going to complain that they have to write a function to do what should 
be a "simple statement". Support all of them and there will probably 
_still_ be someone who wants their own particular set of circumstances 
catered for without having to write their own function to do it.

--

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

(Continue reading)

Andrew Faulds | 19 Aug 2012 00:25

Re: [PHP-DEV] re: removing an item from an array

On 18/08/12 14:52, Morgan L. Owens wrote:
> Rasmus Schultz wrote:
>
> > I disagree - this is (or should be) a simple, atomic operation...
> > yet, you've got a function-call, an intermediary variable, a boolean
> > test, and an unset statement repeating the name of the array you're
> > deleting from.
> >
> > This should be a simple statement or function/method-call, and in
> > most other languages it would be...
>
> How simple is it? Does it:
>
> 1) Remove one occurrence of the element (presumably the first) or all?
> 2) Reindex the array (as someone else argued was necessary to make it 
> "properly indexed" afterwards) or not?
> 3) Modify the array in-place or return a modified array?
> 4) Use type-strict or normal comparisons?
Personally, I like Python's list.remove(x), which I think might be what 
he wants. Certainly it's what I want when I need to remove a list item. 
See: http://docs.python.org/tutorial/datastructures.html*

*According to the docs, it does this: " Remove the first item from the 
list whose value is/x/. It is an error if there is no such item."

So to answer you, 1) one, 2), no, 3) in-place, 4) type-strict (I don't 
see how a weakly-typed comparison for identifying an array value would 
help anyone).
>
> That's sixteen different interpretations of "remove a given value from 
(Continue reading)

Morgan L. Owens | 19 Aug 2012 02:39
Picon
Favicon

Re: [PHP-DEV] re: removing an item from an array

On 2012-08-19 10:25, Andrew Faulds wrote:
> On 18/08/12 14:52, Morgan L. Owens wrote:
>> How simple is it? Does it:
>>
>> 1) Remove one occurrence of the element (presumably the first) or all?
>> 2) Reindex the array (as someone else argued was necessary to make it
>> "properly indexed" afterwards) or not?
>> 3) Modify the array in-place or return a modified array?
>> 4) Use type-strict or normal comparisons?
>
> So to answer you, 1) one, 2), no, 3) in-place, 4) type-strict (I don't
> see how a weakly-typed comparison for identifying an array value would
> help anyone).
So ... using a different definition of "value" than that used by 
array_search() and array_keys(), then?

--

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

Andrew Faulds | 19 Aug 2012 02:43

Re: [PHP-DEV] re: removing an item from an array

On 19/08/12 01:39, Morgan L. Owens wrote:
> On 2012-08-19 10:25, Andrew Faulds wrote:
>> On 18/08/12 14:52, Morgan L. Owens wrote:
>>> How simple is it? Does it:
>>>
>>> 1) Remove one occurrence of the element (presumably the first) or all?
>>> 2) Reindex the array (as someone else argued was necessary to make it
>>> "properly indexed" afterwards) or not?
>>> 3) Modify the array in-place or return a modified array?
>>> 4) Use type-strict or normal comparisons?
>>
>> So to answer you, 1) one, 2), no, 3) in-place, 4) type-strict (I don't
>> see how a weakly-typed comparison for identifying an array value would
>> help anyone).
> So ... using a different definition of "value" than that used by 
> array_search() and array_keys(), then?
>
Assuming you mean the strictness of comparison, you could have an 
optional $strict parameter defaulting to false like array_search(). (and 
hence non-strict by default)

-- 
Andrew Faulds
http://ajf.me/

--

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

(Continue reading)


Gmane