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

2012-08-22 Thread Yasuo Ohgaki
Hi,

2012/8/22 Andrew Faulds :
> Um, Yasuo, have you looked at array_walk implementation and ascertained it
> is safe to change array structure while using it?
>
> Because I'm worried you're changing docs without doing so.

Simply deleting current element is safe.
It's done in everywhere in PHP. It's just like deleting elements with
zend hash.

array_walk() could bite users.
If programmer sort or delete element to be processed next, it causes problem.
PHP programmer will not sort or delete next element to processed while working
with zend hash, but average PHP users do not care about internal.

I finally understand why people use array_filter() to delete elements and
I think we should have array_udelete() since the API is much safer than
array_walk().

Regards,

--
Yasuo Ohgaki
yohg...@ohgaki.net

>
> --
> Sent from Samsung Mobile
> Andrew Faulds
> http://ajf.me/
>
>
>
> Yasuo Ohgaki  wrote:
>
>
> Hi,
>
> 2012/8/22 Levi Morrison :
>> On Tue, Aug 21, 2012 at 7:45 PM, Yasuo Ohgaki  wrote:
>>>
>>> Hi,
>>>
>>> 2012/8/16 Rasmus Schultz :
>>> > 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:
>>> >
>>> >
>>> > http://stackoverflow.com/questions/7225070/php-array-delete-by-value-not-key
>>> >
>>> > Shouldn't we have something simple, like:
>>> >
>>> > array_remove($array, $value) : array (returns a new array)
>>> >
>>> > and/or
>>> >
>>> > array_delete(&$array, $value) : bool (modifies array directly)
>>> >
>>> > ?
>>>
>>> It was amazing that this thread has close to 90 mails.
>>>
>>> I've added some use cases of array_udelete() to wiki, since
>>> some people fails to see how it could be useful. I also bring
>>> back to array_walk() version of equivalent example. I just
>>> don't see any reason why we should replace it with slower
>>> foreach() version.
>>>
>>> I think the RFC page is almost complete.
>>>
>>> https://wiki.php.net/rfc/array_delete
>>>
>>> Any more comments?
>>>
>>> --
>>> Yasuo Ohgaki
>>> yohg...@ohgaki.net
>>
>> Take a look at:
>>
>> http://php.net/manual/en/function.array-walk.php#refsect1-function.array-walk-parameters
>>
>> Look at funcname parameter.  Notice:
>>> Only the values of the array may potentially be changed; its structure
>>> cannot be altered, i.e., the programmer cannot add, unset or reorder
>>> elements. If the callback does not respect this requirement, the behavior of
>>> this function is undefined, and unpredictable.
>>
>> I'm just going to change the behavior back when you switch it to
>> array_walk. It's less clear and technically undefined.
>
> Ok, then my commit to the doc was outstanding still.
> Old PHP was behaved badly with reference variables.
> I'll remove obsolete statement.
>
> I'll fix it later.
> Thank you.
>
> --
> Yasuo Ohgaki
> yohg...@ohgaki.net
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>

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



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

2012-08-21 Thread Andrew Faulds
Um, Yasuo, have you looked at array_walk implementation and ascertained it is 
safe to change array structure while using it?

Because I'm worried you're changing docs without doing so.

--
Sent from Samsung Mobile
Andrew Faulds
http://ajf.me/

Yasuo Ohgaki  wrote:

Hi,

2012/8/22 Levi Morrison :
> On Tue, Aug 21, 2012 at 7:45 PM, Yasuo Ohgaki  wrote:
>>
>> Hi,
>>
>> 2012/8/16 Rasmus Schultz :
>> > 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:
>> >
>> > http://stackoverflow.com/questions/7225070/php-array-delete-by-value-not-key
>> >
>> > Shouldn't we have something simple, like:
>> >
>> > array_remove($array, $value) : array (returns a new array)
>> >
>> > and/or
>> >
>> > array_delete(&$array, $value) : bool (modifies array directly)
>> >
>> > ?
>>
>> It was amazing that this thread has close to 90 mails.
>>
>> I've added some use cases of array_udelete() to wiki, since
>> some people fails to see how it could be useful. I also bring
>> back to array_walk() version of equivalent example. I just
>> don't see any reason why we should replace it with slower
>> foreach() version.
>>
>> I think the RFC page is almost complete.
>>
>> https://wiki.php.net/rfc/array_delete
>>
>> Any more comments?
>>
>> --
>> Yasuo Ohgaki
>> yohg...@ohgaki.net
>
> Take a look at:
> http://php.net/manual/en/function.array-walk.php#refsect1-function.array-walk-parameters
>
> Look at funcname parameter.  Notice:
>> Only the values of the array may potentially be changed; its structure 
>> cannot be altered, i.e., the programmer cannot add, unset or reorder 
>> elements. If the callback does not respect this requirement, the behavior of 
>> this function is undefined, and unpredictable.
>
> I'm just going to change the behavior back when you switch it to
> array_walk. It's less clear and technically undefined.

Ok, then my commit to the doc was outstanding still.
Old PHP was behaved badly with reference variables.
I'll remove obsolete statement.

I'll fix it later.
Thank you.

--
Yasuo Ohgaki
yohg...@ohgaki.net

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



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

2012-08-21 Thread Yasuo Ohgaki
Hi,

2012/8/22 Levi Morrison :
> On Tue, Aug 21, 2012 at 7:45 PM, Yasuo Ohgaki  wrote:
>>
>> Hi,
>>
>> 2012/8/16 Rasmus Schultz :
>> > 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:
>> >
>> > http://stackoverflow.com/questions/7225070/php-array-delete-by-value-not-key
>> >
>> > Shouldn't we have something simple, like:
>> >
>> > array_remove($array, $value) : array (returns a new array)
>> >
>> > and/or
>> >
>> > array_delete(&$array, $value) : bool (modifies array directly)
>> >
>> > ?
>>
>> It was amazing that this thread has close to 90 mails.
>>
>> I've added some use cases of array_udelete() to wiki, since
>> some people fails to see how it could be useful. I also bring
>> back to array_walk() version of equivalent example. I just
>> don't see any reason why we should replace it with slower
>> foreach() version.
>>
>> I think the RFC page is almost complete.
>>
>> https://wiki.php.net/rfc/array_delete
>>
>> Any more comments?
>>
>> --
>> Yasuo Ohgaki
>> yohg...@ohgaki.net
>
> Take a look at:
> http://php.net/manual/en/function.array-walk.php#refsect1-function.array-walk-parameters
>
> Look at funcname parameter.  Notice:
>> Only the values of the array may potentially be changed; its structure 
>> cannot be altered, i.e., the programmer cannot add, unset or reorder 
>> elements. If the callback does not respect this requirement, the behavior of 
>> this function is undefined, and unpredictable.
>
> I'm just going to change the behavior back when you switch it to
> array_walk. It's less clear and technically undefined.

Ok, then my commit to the doc was outstanding still.
Old PHP was behaved badly with reference variables.
I'll remove obsolete statement.

I'll fix it later.
Thank you.

--
Yasuo Ohgaki
yohg...@ohgaki.net

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



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

2012-08-21 Thread Levi Morrison
On Tue, Aug 21, 2012 at 7:45 PM, Yasuo Ohgaki  wrote:
>
> Hi,
>
> 2012/8/16 Rasmus Schultz :
> > 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:
> >
> > http://stackoverflow.com/questions/7225070/php-array-delete-by-value-not-key
> >
> > Shouldn't we have something simple, like:
> >
> > array_remove($array, $value) : array (returns a new array)
> >
> > and/or
> >
> > array_delete(&$array, $value) : bool (modifies array directly)
> >
> > ?
>
> It was amazing that this thread has close to 90 mails.
>
> I've added some use cases of array_udelete() to wiki, since
> some people fails to see how it could be useful. I also bring
> back to array_walk() version of equivalent example. I just
> don't see any reason why we should replace it with slower
> foreach() version.
>
> I think the RFC page is almost complete.
>
> https://wiki.php.net/rfc/array_delete
>
> Any more comments?
>
> --
> Yasuo Ohgaki
> yohg...@ohgaki.net

Take a look at:
http://php.net/manual/en/function.array-walk.php#refsect1-function.array-walk-parameters

Look at funcname parameter.  Notice:
> Only the values of the array may potentially be changed; its structure cannot 
> be altered, i.e., the programmer cannot add, unset or reorder elements. If 
> the callback does not respect this requirement, the behavior of this function 
> is undefined, and unpredictable.

I'm just going to change the behavior back when you switch it to
array_walk. It's less clear and technically undefined.

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



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

2012-08-21 Thread Yasuo Ohgaki
Hi,

2012/8/16 Rasmus Schultz :
> 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:
>
> http://stackoverflow.com/questions/7225070/php-array-delete-by-value-not-key
>
> Shouldn't we have something simple, like:
>
> array_remove($array, $value) : array (returns a new array)
>
> and/or
>
> array_delete(&$array, $value) : bool (modifies array directly)
>
> ?

It was amazing that this thread has close to 90 mails.

I've added some use cases of array_udelete() to wiki, since
some people fails to see how it could be useful. I also bring
back to array_walk() version of equivalent example. I just
don't see any reason why we should replace it with slower
foreach() version.

I think the RFC page is almost complete.

https://wiki.php.net/rfc/array_delete

Any more comments?

--
Yasuo Ohgaki
yohg...@ohgaki.net

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



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

2012-08-20 Thread Rasmus Schultz
I think adding more collection-types is the intuitive reaction to this
issue, but there's something to be said for the idea of having only a
single collection-type - I think that's one PHP feature we should not give
up.

Not having to pick and choose (and compromise) to select the "right"
collection-type, and not having to refactor when you realize you needed
another collection-type - as well as easy comprehension for developers...
these are valuable aspects of having only a single collection-type. Some
newer languages like Opa embrace that idea with great elegance. Giving up
on that idea should be the last option, in my opinion.

There are plenty of cases for collections of objects that do not have a
scalar key, where the key is indeterminate, or where the key can change -
and thus cannot have known indexes.

As an aside note, I recently benchmarked array_search() for a project that
needs to store many different types of objects in a list - and searching a
list with 1000 objects for one specific object is extremely fast: a couple
of micro-seconds more (per search) when compared against a hash-based
lookup with a known key, so (in my case) nothing to worry about at all in
terms of performance.

Arrays are a powerful and pure feature in PHP - I would vote against
introducing more collection-types, and instead leverage the already
powerful and well-understood existing singular collection-type.


On Sat, Aug 18, 2012 at 2:42 AM, Alexey Zakhlestin wrote:

>
> On 16.08.2012, at 0:18, Rasmus Schultz  wrote:
>
> > How come there is no straight-foward obvious way to simply remove a given
> > value from an array?
>
> Well, this sounds like a reason for creating SplSet class
>
>


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

2012-08-19 Thread Lars Schultz

Sorry for creating more "noise"...

Am 18.08.2012 06:59, schrieb Sherif Ramadan:

Further more, your assumption here that the result is incorrect (or
improperly indexed) is completely baseless and moot. You are making a
bold assumption that I both care about the keys and that I expect them
to be numeric and sequential (why can't I have string keys?). This
would also entail that I depend on keys for order (which is not a
requirement for a PHP Array since all PHP Arrays are
ordered-hashmaps).


I don't assume anything. I only stated a use-case where unset() will not 
work. I neatly listed some solutions and pointed out their 
merits...mostly to show that there are less than ideal solutions out 
there that will either perform badly or worse, not work properly at all.



The order of the elements in the array has not been affected by the
keys that index them or the use of unset() in the least. So your
argument holds no merit. If you are depending on the sequence of the
keys to determine the order of your elements you made a huge mistake,
because the array was already designed to store elements in order
regardless of whether you provide a key or not. So you're solving the
wrong problem here.


Rest assured, I do know how PHP arrays work. I don't see what the 
ordering has to do with wanting to keep an array sequentially-indexed.



I never said it was redundant or fussy.

I am going to stop here, for the sake of noiselessness.


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



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

2012-08-18 Thread Morgan L. Owens

On 2012-08-19 04:08, Levi Morrison wrote:

On Sat, Aug 18, 2012 at 12:42 AM, Alexey Zakhlestin  wrote:


On 16.08.2012, at 0:18, Rasmus Schultz  wrote:


How come there is no straight-foward obvious way to simply remove a given
value from an array?


Well, this sounds like a reason for creating SplSet class



There's already SplObjectStorage which CAN act like a Set for objects
only.  It's a terrible solution in my opinion and am working on
creating a proper one.  I don't know if that effort will be accepted,
but I wanted to point out that a set already exists in the SPL.


SplObjectStorage would indeed cover one given example:

On 2012-08-19 10:18, Andrew Faulds wrote:
> Hmm. I can think of a particular instance I've needed to remove an
> item from an array by value, and where keys wouldn't be an option. In
> PHP you can only have string or int keys, you can't have object keys.
> In Python, I've used Twisted (an excellent asynchronous OOP
> networking framework) to write client-server applications. One thing
> I did was maintain a list of clients (that is, a Python list). When a
> client disconnected, I would do list.remove(client). Of course there
> might be better ways to implement this, but it was simple and worked.
> Anything wrong with allowing the same in PHP?
>
SplObjectStorage even has a detach() method for removing an element by 
value, as well as implementing the various array access interfaces.




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



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

2012-08-18 Thread Levi Morrison
On Sat, Aug 18, 2012 at 12:42 AM, Alexey Zakhlestin  wrote:
>
> On 16.08.2012, at 0:18, Rasmus Schultz  wrote:
>
>> How come there is no straight-foward obvious way to simply remove a given
>> value from an array?
>
> Well, this sounds like a reason for creating SplSet class
>

There's already SplObjectStorage which CAN act like a Set for objects
only.  It's a terrible solution in my opinion and am working on
creating a proper one.  I don't know if that effort will be accepted,
but I wanted to point out that a set already exists in the SPL.

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



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

2012-08-17 Thread Alexey Zakhlestin

On 16.08.2012, at 0:18, Rasmus Schultz  wrote:

> How come there is no straight-foward obvious way to simply remove a given
> value from an array?

Well, this sounds like a reason for creating SplSet class


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



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

2012-08-17 Thread Sherif Ramadan
On Thu, Aug 16, 2012 at 2:39 AM, Lars Schultz  wrote:
> Am 16.08.2012 07:55, schrieb Sherif Ramadan:
>
>> Now your array is something completely different from what you wanted.
>> The solution stated earlier is the most sane one (just using
>> array_keys() with a search value).
>
>
> the array_keys solution is slower (although barely) than my suggested
> array_slice solution but comes up short when looking at the result closely.
> The resulting array won't be properly indexed anymore, but will be a
> numbered hashmap. I am not sure wether PHP keeps an internal state on wether
> an array is a map or a classic array, but when you require indizes to be
> sequential (without gaps) then unset() just won't do. Thus it's not always a
> simple problem.
>


Efficiency is only secondary to effectiveness. Either the
function/implementation does what you want or it doesn't.

Further more, your assumption here that the result is incorrect (or
improperly indexed) is completely baseless and moot. You are making a
bold assumption that I both care about the keys and that I expect them
to be numeric and sequential (why can't I have string keys?). This
would also entail that I depend on keys for order (which is not a
requirement for a PHP Array since all PHP Arrays are
ordered-hashmaps).

$array = (99=>1, 7=>2, 701=>3);
foreach ($array as $v) {
  echo "$v\n";
}
/* Output is as expected
1
2
3
*/

unset($array[7]);

foreach ($array as $v) {
  echo "$v\n";
}
/* Output is still as expected
1
3
*/

The order of the elements in the array has not been affected by the
keys that index them or the use of unset() in the least. So your
argument holds no merit. If you are depending on the sequence of the
keys to determine the order of your elements you made a huge mistake,
because the array was already designed to store elements in order
regardless of whether you provide a key or not. So you're solving the
wrong problem here.

>
>> I don't wish to degrade anyone's contributions to this thread, but
>> this really is the perfect example of making a lot of fuss over
>> nothing on the mailing list and an example of the kinds of discussion
>> we should be avoiding when there are so many other important problems
>> we can solve.
>
>
> Rasmus' suggestion was very concise and unfussy. I've been wondering the
> same thing for some time. It would make the array-functionset more complete
> and code more explicit. Why should we only talk about major changes and
> additions?
>
>

I never said it was redundant or fussy.

Here you're advocating that you're "for" this feature and yet just a
few posts earlier you explicitly state (and I quote):

"I am not arguing for another array-function (as there are so many already)"

This is exactly the kind of noise I see people making in these types
of threads. On one hand they argue indifference (and yet they're here
making all sorts of claims), and on the other hand they want to argue
for arguments sake (like where they're contradicting themselves like
you have).

On one hand you're saying "It would make the array-functionset more
complete" and on the other hand you're also saying "...there are so
many (array functions) already..."

Which is it?

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



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

2012-08-16 Thread Lester Caine

Sherif Ramadan wrote:

I don't wish to degrade anyone's contributions to this thread, but
this really is the perfect example of making a lot of fuss over
nothing on the mailing list and an example of the kinds of discussion
we should be avoiding when there are so many other important problems
we can solve.


While discussing use cases may be better done on the general list ... where it 
will help users understand the differences ... Lars comparisons of approaches is 
an ideal example of an internals discussion over whether an extra shortcut is 
needed and how it might be implemented.


Given some of the discussions here on how to remove a few characters from 
something that IS irrelevant, it's nice to see a thread that I can follow, 
understand, and does contribute to tidying and improving the performance of 
code. The arrays API is one area that has been flagged as needing rationalising 
and Lars examples are good explanation of why?


--
Lester Caine - G8HFL
-
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk
Rainbow Digital Media - http://rainbowdigitalmedia.co.uk



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



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

2012-08-15 Thread Morgan L. Owens

On 2012-08-16 17:55, Sherif Ramadan wrote:
> That doesn't make any sense. What if the values are present more than
> once? array_flip will cause the keys to be overwritten.
>
Not to mention converting all of the array's elements to strings and/or 
integers.




Now your array is something completely different from what you wanted.
The solution stated earlier is the most sane one (just using
array_keys() with a search value).

The problem isn't very complicated and doesn't require a complex solution.

This thread is overstating a rudimentary problem (and that's the lack
of understanding PHP arrays).


I was actually alluding to something more disturbing:

...which makes array_diff and its ilk only useful for scalar elements 
(hence the need for the more complicated approach using array_keys(), 
which provides a strictness flag).




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



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

2012-08-15 Thread Lars Schultz

Am 16.08.2012 07:55, schrieb Sherif Ramadan:

Now your array is something completely different from what you wanted.
The solution stated earlier is the most sane one (just using
array_keys() with a search value).


the array_keys solution is slower (although barely) than my suggested 
array_slice solution but comes up short when looking at the result 
closely. The resulting array won't be properly indexed anymore, but will 
be a numbered hashmap. I am not sure wether PHP keeps an internal state 
on wether an array is a map or a classic array, but when you require 
indizes to be sequential (without gaps) then unset() just won't do. Thus 
it's not always a simple problem.



I don't wish to degrade anyone's contributions to this thread, but
this really is the perfect example of making a lot of fuss over
nothing on the mailing list and an example of the kinds of discussion
we should be avoiding when there are so many other important problems
we can solve.


Rasmus' suggestion was very concise and unfussy. I've been wondering the 
same thing for some time. It would make the array-functionset more 
complete and code more explicit. Why should we only talk about major 
changes and additions?



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



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

2012-08-15 Thread Sherif Ramadan
>
> 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);


That doesn't make any sense. What if the values are present more than
once? array_flip will cause the keys to be overwritten.

$array = array('foo','bar','baz','baz');
$flipped_array = array_flip($array);
unset($flipped_array['foo']);
$array = array_flip($flipped_array);
var_dump($array);

Now your array is something completely different from what you wanted.
The solution stated earlier is the most sane one (just using
array_keys() with a search value).

The problem isn't very complicated and doesn't require a complex solution.

This thread is overstating a rudimentary problem (and that's the lack
of understanding PHP arrays).

Unlike most other languages PHP's arrays aren't really arrays, because
they don't create a list of values, but instead create an ordered
hashmap, which in-turn solves a wide variety of general problems such
as the ability to create dictionaries as well as ordered lists, which
-- when combining all general use cases that the PHP array aims to
solve -- is otherwise going to require having additional multiple
primitives for each use case.

For example in Python you need a combination of Tuples and Arrays to
achieve similar map structures. PHP aims to make this a more
simplified general use case primitive by abstracting away most of this
low-level work for you in the user-space code.

I don't wish to degrade anyone's contributions to this thread, but
this really is the perfect example of making a lot of fuss over
nothing on the mailing list and an example of the kinds of discussion
we should be avoiding when there are so many other important problems
we can solve.

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



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

2012-08-15 Thread Lars Schultz

Am 15.08.2012 22:22, schrieb Stas Malyshev:

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.

One thing that should be noted in this case and any solution that relies 
on unset() is that even though its simple and fast, it will not result 
in a properly indexed array. The same goes for any array_diff based 
solution.


I tried and compared the following solutions and ordered them according 
to their performance. The fastest (and with a correct result) solution 
is based on array_slice. Why this is the case I can not say...I am not 
arguing for another array-function (as there are so many already)...but 
I certainly have my own array_remove implementation, since it's such a 
common use-case.


function array_remove_slice(&$haystack,$needle){
while ( true ) {
$pos = array_search($needle,$haystack,true);
if ( $pos === false ) return;

$haystack = array_merge(
array_slice($haystack,0,$pos) ,
array_slice($haystack,$pos+1)
);
}
}

/* ~1.5 times slower than slice */
function array_remove_unset(&$haystack,$needle){
while ( true ) {
$pos = array_search($needle,$haystack,true);
if ( $pos === false ) break;

unset($haystack[$pos]);
}
}

/* ~2.3 times slower than slice */
function array_remove_loop(&$haystack,$needle){
$result = array();
foreach( $haystack as $value ) {
if ( $needle == $value ) continue;
$result[] = $value;
}
$haystack = $result;
}


/* ~3.5 times slower than slice */
function array_remove_diff(&$haystack,$needle){
$haystack = array_diff($haystack,array($needle));
}


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



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

2012-08-15 Thread Tyler L
On Wed, Aug 15, 2012 at 7:59 PM, Morgan L. Owens wrote:

> On 2012-08-16 08:27, Nikita Popov wrote:
>
>> On Wed, Aug 15, 2012 at 10:22 PM, Stas Malyshev 
>> 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);


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

2012-08-15 Thread Morgan L. Owens

On 2012-08-16 08:27, Nikita Popov wrote:

On Wed, Aug 15, 2012 at 10:22 PM, Stas Malyshev  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



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

2012-08-15 Thread Kris Craig
On Wed, Aug 15, 2012 at 1:35 PM, Kris Craig  wrote:

>
>
> On Wed, Aug 15, 2012 at 1:31 PM, Nikita Popov wrote:
>
>> On Wed, Aug 15, 2012 at 10:29 PM, Kris Craig 
>> wrote:
>> >>
>> >> Btw, deleting all values (not just the first) is also very easy
>> currently:
>> >>
>> >> foreach (array_keys($array, $delValue) as $key) {
>> >> unset($array[$key]);
>> >> }
>> >>
>> >
>> > Even easier still, just do this:
>> >
>> > $array_var = array();
>>
>> It's often overlooked, but array_keys has a second parameter that only
>> returns the keys for a certain value: http://php.net/array_keys ;) So
>> no, that does not clean off the whole array.
>>
>> Nikita
>>
>
> If you re-initialize it by setting it to array(), then yes that most
> definitely will clear all the values in the array.  As far as I know,
> array_keys() has nothing to do with that.
>
> --Kris
>
>
Err nevermind, I think I misread what you were trying to do.  If you want
to only clear a certain value, then yes using array_keys() with a search
value specified is the way to go.  If you want to clear all values in the
array period (which is what I thought you were saying), then
re-initializing with array() makes the most sense.

--Kris


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

2012-08-15 Thread Kris Craig
On Wed, Aug 15, 2012 at 1:31 PM, Nikita Popov  wrote:

> On Wed, Aug 15, 2012 at 10:29 PM, Kris Craig  wrote:
> >>
> >> Btw, deleting all values (not just the first) is also very easy
> currently:
> >>
> >> foreach (array_keys($array, $delValue) as $key) {
> >> unset($array[$key]);
> >> }
> >>
> >
> > Even easier still, just do this:
> >
> > $array_var = array();
>
> It's often overlooked, but array_keys has a second parameter that only
> returns the keys for a certain value: http://php.net/array_keys ;) So
> no, that does not clean off the whole array.
>
> Nikita
>

If you re-initialize it by setting it to array(), then yes that most
definitely will clear all the values in the array.  As far as I know,
array_keys() has nothing to do with that.

--Kris


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

2012-08-15 Thread Nikita Popov
On Wed, Aug 15, 2012 at 10:29 PM, Kris Craig  wrote:
>>
>> Btw, deleting all values (not just the first) is also very easy currently:
>>
>> foreach (array_keys($array, $delValue) as $key) {
>> unset($array[$key]);
>> }
>>
>
> Even easier still, just do this:
>
> $array_var = array();

It's often overlooked, but array_keys has a second parameter that only
returns the keys for a certain value: http://php.net/array_keys ;) So
no, that does not clean off the whole array.

Nikita

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



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

2012-08-15 Thread Kris Craig
>
>
> Btw, deleting all values (not just the first) is also very easy currently:
>
> foreach (array_keys($array, $delValue) as $key) {
> unset($array[$key]);
> }
>
>
Even easier still, just do this:

$array_var = array();

--Kris


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

2012-08-15 Thread Nikita Popov
On Wed, Aug 15, 2012 at 10:22 PM, Stas Malyshev  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



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

2012-08-15 Thread Will Fitch
I like that chose 42 for the value.  You win, and I completely agree.

On Wed, Aug 15, 2012 at 4:22 PM, Stas Malyshev 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.
>
> --
> Stanislav Malyshev, Software Architect
> SugarCRM: http://www.sugarcrm.com/
> (408)454-6900 ext. 227
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


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

2012-08-15 Thread Stas Malyshev
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.

-- 
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227

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



[PHP-DEV] removing an item from an array

2012-08-15 Thread Rasmus Schultz
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:

http://stackoverflow.com/questions/7225070/php-array-delete-by-value-not-key

Shouldn't we have something simple, like:

array_remove($array, $value) : array (returns a new array)

and/or

array_delete(&$array, $value) : bool (modifies array directly)

?