# removing an item from an array

- Thread: https://externals.io/message/62224
- Original archive: https://news-web.php.net/php.internals/62224
- Started by Rasmus Schultz on 2012-08-17T21:21:12+00:00
- Messages: 61
- Page 1 of 3: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/62224.md?page=2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #62224 — Rasmus Schultz — 2012-08-17T21:21:12+00:00

URL: https://externals.io/message/62224#62224

>
> if(($key = array_search($del_val, $messages)) !== false) {
>     unset($messages[$key]);
> }
>
> Nothing horrible here.
>

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...

---

## #62225 — Rasmus Lerdorf — 2012-08-17T21:23:51+00:00

URL: https://externals.io/message/62224#62225 · In reply to: #62224

> > […]
> 
> 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...

Really? I can't think of a single language that has a call to remove an
element by value in a key-value hash. Do you have some examples? What do
you do with duplicates?

-Rasmus

---

## #62226 — Rasmus Schultz — 2012-08-17T21:35:46+00:00

URL: https://externals.io/message/62224#62226 · In reply to: #62225

Most other languages have more than one collection-type... since PHP has
only the single, hybrid array-type which acts both as a hash and as an
array, something like this ought to be available.

I don't know why everyone is so eager to jump up and argue against
something this simple, basic and useful. The fact that this is missing is
an oversight - just look at the number of solutions people come up with.
For what?

I want to remove and object from a list. Not an exotic and crazy
requirement, is it? It just isn't the sort of thing that should require any
substantial thinking or typing.

And it doesn't help make codebases more legible when people come up with 25
different ways to do it.



> 
> > […]
> test,
> > […]
> from.
> > […]
>
> Really? I can't think of a single language that has a call to remove an
> element by value in a key-value hash. Do you have some examples? What do
> you do with duplicates?
>
> -Rasmus

---

## #62227 — Rasmus Lerdorf — 2012-08-17T21:41:02+00:00

URL: https://externals.io/message/62224#62227 · In reply to: #62226

> Most other languages have more than one collection-type... since PHP has
> only the single, hybrid array-type which acts both as a hash and as an
> array, something like this ought to be available.
> 
> I don't know why everyone is so eager to jump up and argue against
> something this simple, basic and useful. The fact that this is missing
> is an oversight - just look at the number of solutions people come up
> with. For what?
> 
> I want to remove and object from a list. Not an exotic and crazy
> requirement, is it? It just isn't the sort of thing that should require
> any substantial thinking or typing.
> 
> And it doesn't help make codebases more legible when people come up with
> 25 different ways to do it.

The problem is that it is a rather unusual thing to do. I don't mean
removing an element, that is very common, but you are implying that you
know you don't have duplicates and you want a fast way to remove an
element by value in that case. To me that means you built your array
badly. If the values are unique then they should be the keys, or at
least a representation of the value should be the key such that you
don't need to scan the entire hash for the element when you need to
access it. And removal is just like any other access.

-Rasmus

---

## #62229 — Nikita Popov — 2012-08-17T21:50:38+00:00

URL: https://externals.io/message/62224#62229 · In reply to: #62226

> Most other languages have more than one collection-type... since PHP has
> only the single, hybrid array-type which acts both as a hash and as an
> array, something like this ought to be available.
>
> I don't know why everyone is so eager to jump up and argue against
> something this simple, basic and useful. The fact that this is missing is
> an oversight - just look at the number of solutions people come up with.
> For what?
Could we please stop these pseudo-arguments? "simple, basic and
useful. The fact that this is missing is an oversight"? Do you have
anything to back up these claims? I can't remember to have been in a
situation where I wanted to remove a value from an array. Simply
because that's a slow operation. When you have to do it you *usually*
have chosen the wrong datastructure. And in the cases where you do
need it, you can easily do it with array_search. Where is the issue?

Also, as I already pointed out, "removing a value from an array" is
not well defined. Do you mean to only remove the first occurrence of
the value? Only the last? All of them? Do you want to have special
functions for all of those?

Furthermore, why do you keep coming back to the "There exists more
than one solution to this problem" argument? Everything can be done in
a large number of ways. And people come up with a lot of ways,
depending on the situation. You can output a string with echo, with
print, with concatenation, with interpolation, with multi-arg echo,
with printf, etc. So sure, there are also multiple ways to remove
values from an array. What's the issue with that?

Nikita

---

## #62248 — Andrew Faulds — 2012-08-18T22:18:37+00:00

URL: https://externals.io/message/62224#62248 · In reply to: #62227

> 
> > […]
> The problem is that it is a rather unusual thing to do. I don't mean
> removing an element, that is very common, but you are implying that you
> know you don't have duplicates and you want a fast way to remove an
> element by value in that case. To me that means you built your array
> badly. If the values are unique then they should be the keys, or at
> least a representation of the value should be the key such that you
> don't need to scan the entire hash for the element when you need to
> access it. And removal is just like any other access.
>
> -Rasmus
>
>
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?

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

---

## #62265 — Yasuo Ohgaki — 2012-08-19T21:57:27+00:00

URL: https://externals.io/message/62224#62265 · In reply to: #62225

2012/8/18 Rasmus Lerdorf <rasmus@lerdorf.com>:
> 
> > […]
>
> Really? I can't think of a single language that has a call to remove an
> element by value in a key-value hash. Do you have some examples? What do
> you do with duplicates?
>

Ruby can do (using irb)

ruby-1.9.2-p180 :007 > h = {"apple"=>150, "banana"=>300, "lemon"=>300}
 => {"apple"=>150, "banana"=>300, "lemon"=>300}
ruby-1.9.2-p180 :008 > h.delete_if { |k,v| v==300 }
 => {"apple"=>150}

May be we should have something like

array_delete_if($array, function($v, $k=null) { if ($v == 300) return true; })

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #62266 — Etienne Kneuss — 2012-08-19T22:19:25+00:00

URL: https://externals.io/message/62224#62266 · In reply to: #62265

> 2012/8/18 Rasmus Lerdorf <rasmus@lerdorf.com>:
> > […]
>
> Ruby can do (using irb)
>
> ruby-1.9.2-p180 :007 > h = {"apple"=>150, "banana"=>300, "lemon"=>300}
>  => {"apple"=>150, "banana"=>300, "lemon"=>300}
> ruby-1.9.2-p180 :008 > h.delete_if { |k,v| v==300 }
>  => {"apple"=>150}
>
> May be we should have something like
>
> array_delete_if($array, function($v, $k=null) { if ($v == 300) return true; })

So array_filter?

>
> --
> Yasuo Ohgaki
> yohgaki@ohgaki.net
>
> --
> 
> 
>



-- 
Etienne Kneuss
http://www.colder.ch

---

## #62267 — Yasuo Ohgaki — 2012-08-19T23:05:51+00:00

URL: https://externals.io/message/62224#62267 · In reply to: #62266

2012/8/20 Etienne Kneuss <colder@php.net>:
> 
> > […]
>
> So array_filter?

I'll use it or like for deleting, but the point of this thread is
"intuitive function for deleting element(s)"

array_delete($array, $value|callable)

would be nicer for users, perhaps.

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #62269 — Andrew Faulds — 2012-08-19T23:08:05+00:00

URL: https://externals.io/message/62224#62269 · In reply to: #62267

> 2012/8/20 Etienne Kneuss <colder@php.net>:
> > […]
> I'll use it or like for deleting, but the point of this thread is
> "intuitive function for deleting element(s)"
>
> array_delete($array, $value|callable)
>
> would be nicer for users, perhaps.
A callable? Wouldn't that mean you couldn't delete strings? :(
>
> --
> Yasuo Ohgaki
> yohgaki@ohgaki.net
>


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

---

## #62270 — Yasuo Ohgaki — 2012-08-19T23:16:14+00:00

URL: https://externals.io/message/62224#62270 · In reply to: #62269

2012/8/20 Andrew Faulds <ajf@ajf.me>:
> 
> > […]
>
> A callable? Wouldn't that mean you couldn't delete strings? :(

If you read my previous post,  you'll see why it would be nicer with callable.

> > […]

--
Yasuo Ohgaki

---

## #62271 — Andrew Faulds — 2012-08-19T23:19:02+00:00

URL: https://externals.io/message/62224#62271 · In reply to: #62270

> 2012/8/20 Andrew Faulds <ajf@ajf.me>:
> > […]
> If you read my previous post,  you'll see why it would be nicer with callable.
Being unable to remove callables from an array would be very strange, 
and I think for the use case you presented array_filter works fine.
> > […]
> --
> Yasuo Ohgaki


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

---

## #62275 — Yasuo Ohgaki — 2012-08-20T01:32:21+00:00

URL: https://externals.io/message/62224#62275 · In reply to: #62271

2012/8/20 Andrew Faulds <ajf@ajf.me>:
> 
> > […]
>
> Being unable to remove callables from an array would be very strange, and I
> think for the use case you presented array_filter works fine.

Of course it works.

$array = array_filter($array, function($v) use ($del_val) {
    return ($v !== $del_val);
});

It does not delete element(s), but creates new array.

void array_delete ( array &$input [, callable $callback = "" |
$values_to_be_deleted ] )

This will delete element(s).
We can use `array_walk()` to delete element(s) from array also.

php > $array = array(1,2,3,4,5);
php > array_walk($array, function($val, $key) use (&$array) {if ($val
== 3) unset($array[$key]);});
php > var_dump($array);
array(4) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [3]=>
  int(4)
  [4]=>
  int(5)
}

I suppose this isn't an intuitive way for novices because stack
overflow's answers
didn't have this method that actually delete element(s) from an array.

Adding an intuitive element deletion function is the point of this
thread, isn't it?
I'll vote +1 for adding array_delete().

Regards,

--
Yasuo Ohgaki

>
> > […]
>
>
>
> --
> Andrew Faulds
> http://ajf.me/

---

## #62290 — Rasmus Schultz — 2012-08-20T15:36:50+00:00

URL: https://externals.io/message/62224#62290 · In reply to: #62229

Absolutely, you're right. I have a tendency to get dragged into those.

I apologize.



>
> Could we please stop these pseudo-arguments?

---

## #62291 — Herman Radtke — 2012-08-20T16:47:42+00:00

URL: https://externals.io/message/62224#62291 · In reply to: #62267

> > […]

> > […]
> true; })
> > […]
>
> I'll use it or like for deleting, but the point of this thread is
> "intuitive function for deleting element(s)"
>
> array_delete($array, $value|callable)
>
> would be nicer for users, perhaps.


You are basically asking to alias array_filter with "array_delete". That is
a very slippery slope. I think array_filter is very a very obvious choice
to remove something from an array. The "filter" function/method is common
in functional languages (and functional frameworks like Underscore).

These are things developers just need to learn as part of development.
Really, this is entire thread should be on stack overflow, not internals.

-- 
Herman Radtke
hermanradtke@gmail.com | http://hermanradtke.com

---

## #62292 — Andrew Faulds — 2012-08-20T17:00:01+00:00

URL: https://externals.io/message/62224#62292 · In reply to: #62291

> > […]
>
> You are basically asking to alias array_filter with "array_delete". That is
> a very slippery slope. I think array_filter is very a very obvious choice
> to remove something from an array. The "filter" function/method is common
> in functional languages (and functional frameworks like Underscore).
>
> These are things developers just need to learn as part of development.
> Really, this is entire thread should be on stack overflow, not internals.
>
You seem a little arrogant. Arguably, a lot of array functions are 
unnecessary. But PHP is not supposed to be an extremely minimal 
language. Convenience functions are a good thing.

We have array_pad for instance: completely unnecessary, you can do it 
manually with other array functions. Array_fill_keys and array_fill, 
too. array_flip is also unnecessary. Heck, who needs sorting, you can do 
that manually too.

array_delete would be a more convenient, and more readable way to remove 
a value from an array. Yes, you can already remove array items, that's 
not the point. The point is this way is simpler and more convenient.

Also, as someone else mentioned, PHP's universal collection datatype is 
a great thing. It is an associative array, it is a list, it is a tuple, 
it is a set, it can be used in many ways, it's incredibly versatile. 
Adding array_delete would allow you to use it like a set more easily.

Just my 2½¢ :)

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

---

## #62293 — Levi Morrison — 2012-08-20T17:11:36+00:00

URL: https://externals.io/message/62224#62293 · In reply to: #62292

> Adding array_delete would allow you to use it like a set more

It's not a set and wasn't meant to be.  If you want really bad set
behavior, feel free to use an array as one.

---

## #62294 — Andrew Faulds — 2012-08-20T17:13:10+00:00

URL: https://externals.io/message/62224#62294 · In reply to: #62293

> > […]
> It's not a set and wasn't meant to be.  If you want really bad set
> behavior, feel free to use an array as one.
Arrays also aren't lists or associative arrays, strictly speaking.

And there is another use case: removing of list items by value, instead 
of key.

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

---

## #62295 — Levi Morrison — 2012-08-20T17:27:06+00:00

URL: https://externals.io/message/62224#62295 · In reply to: #62292

> You are basically asking to alias array_filter with "array_delete". That is
> a very slippery slope. I think array_filter is a very obvious choice
> to remove something from an array. The "filter" function/method is common
> in functional languages (and functional frameworks like Underscore).

This about sums up my opinion on deleting things by value on a PHP array.

---

## #62296 — Andrew Faulds — 2012-08-20T17:41:47+00:00

URL: https://externals.io/message/62224#62296 · In reply to: #62295

> > […]
> This about sums up my opinion on deleting things by value on a PHP array.
** Me and Levi had a private email back-and-forth about this. Like many 
things here, this discussion has become a pseudo-argument, and it's 
unclear whether people actually think it's a good idea or not. So let's 
make an RFC (I think array_delete($array, $value, $all=false), mirrors 
Python's replace-one-only by default), vote on it, and that can decide 
things. So we can avoid argument. Don't like the syntax? Vote against 
it, but I don't see how it could be much better.

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

---

## #62297 — Sebastian Krebs — 2012-08-20T17:43:15+00:00

URL: https://externals.io/message/62224#62297 · In reply to: #62292

Am 20.08.2012 19:00, schrieb Andrew Faulds:
> 
> > […]
> You seem a little arrogant. Arguably, a lot of array functions are
> unnecessary. But PHP is not supposed to be an extremely minimal
> language. Convenience functions are a good thing.
>
> We have array_pad for instance: completely unnecessary, you can do it
> manually with other array functions. Array_fill_keys and array_fill,
> too. array_flip is also unnecessary. Heck, who needs sorting, you can do
> that manually too.
>
> array_delete would be a more convenient, and more readable way to remove
> a value from an array. Yes, you can already remove array items, that's
> not the point. The point is this way is simpler and more convenient.

So you just want a convenient and readable way and you realise yourself, 
that it is really simple to implement. So: Why don't you just implement 
it yourself?


function array_delete($array, $value) {
   // Your code here
}


What I don't understand is, why should every function goes directly into 
the core, if you can achieve exactly the same without core changes?

Regards,
Sebastian

>
> Also, as someone else mentioned, PHP's universal collection datatype is
> a great thing. It is an associative array, it is a list, it is a tuple,
> it is a set, it can be used in many ways, it's incredibly versatile.
> Adding array_delete would allow you to use it like a set more easily.
>
> Just my 2½¢ :)

---

## #62298 — Andrew Faulds — 2012-08-20T17:44:45+00:00

URL: https://externals.io/message/62224#62298 · In reply to: #62297

> Am 20.08.2012 19:00, schrieb Andrew Faulds:
> > […]
>
> So you just want a convenient and readable way and you realise 
> yourself, that it is really simple to implement. So: Why don't you 
> just implement it yourself?
>
>
> function array_delete($array, $value) {
>   // Your code here
> }
>
>
> What I don't understand is, why should every function goes directly 
> into the core, if you can achieve exactly the same without core changes?
>
I'd rather not argue any further about this (let's make an RFC and 
vote). I've already addressed this point.

> Regards,
> Sebastian
>
> > […]
>
>


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

---

## #62299 — Levi Morrison — 2012-08-20T18:05:42+00:00

URL: https://externals.io/message/62224#62299 · In reply to: #62295

Some major points to consider for `array_delete`'s behavior:

1. Should items be compared with `==`, `===`, or custom comparison? If
we use `==` or `===` we'd probably add `array_udelete` to allow a
custom comparator.
2. Should it stop when it encounters the first value that matches?  If
it does, should we add a function that searches in the reverse
direction?
3. Should it modify the array in-place? If so, should we have another
function that returns a copy of the array that does not include the
removed value(s) instead?

If someone wants to go through and define all of these cases and
propose a patch, more power to them.

-----

Here are some reasons that aren't related to the above as to why I'm
against adding `array_delete`:

1. PHP arrays are not sets. PHP arrays are meant to be a list and an
associative array. As such, a PHP array can act as any* data structure
that can be built from a list or associative array.  A data structure
that removes something by value is typically associated with a set and
therefore does not belong in an array.
2. Using other array functions cover this use case AND do a better
job.  Want to remove the first instance of the value? Use
`array_search` to find the index and unset it. If you want to reorder
the array you can use `array_search` to find the index and use
`array_slice`.  Want to remove all instances of the value in the
array? Use `array_filter`.

---

## #62300 — Andrew Faulds — 2012-08-20T18:07:41+00:00

URL: https://externals.io/message/62224#62300 · In reply to: #62299

> Some major points to consider for `array_delete`'s behavior:
>
> 1. Should items be compared with `==`, `===`, or custom comparison? If
> we use `==` or `===` we'd probably add `array_udelete` to allow a
> custom comparator.
array_delete($array, $value, $all=false, $strict=false)
> 2. Should it stop when it encounters the first value that matches?  If
> it does, should we add a function that searches in the reverse
> direction?
^
> 3. Should it modify the array in-place? If so, should we have another
> function that returns a copy of the array that does not include the
> removed value(s) instead?
Modify in-place. No need for another, that's filter's job imo.
>
> If someone wants to go through and define all of these cases and
> propose a patch, more power to them.
Might do so.
>
> -----
>
> Here are some reasons that aren't related to the above as to why I'm
> against adding `array_delete`:
>
> 1. PHP arrays are not sets. PHP arrays are meant to be a list and an
> associative array. As such, a PHP array can act as any* data structure
> that can be built from a list or associative array.  A data structure
> that removes something by value is typically associated with a set and
> therefore does not belong in an array.
> 2. Using other array functions cover this use case AND do a better
> job.  Want to remove the first instance of the value? Use
> `array_search` to find the index and unset it. If you want to reorder
> the array you can use `array_search` to find the index and use
> `array_slice`.  Want to remove all instances of the value in the
> array? Use `array_filter`.


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

---

## #62301 — Levi Morrison — 2012-08-20T18:08:52+00:00

URL: https://externals.io/message/62224#62301 · In reply to: #62299

Also, `array_splice` will remove the index while modifying the array in-place.

---

Next page: https://externals.io/message/62224.md?page=2
