Web lists-archives.com

Re: [PHP] xarray - The missing array functions you are looking for, implemented in Extension




On Oct 15, 2015, at 06:44, Ryan Pallas <derokorian@xxxxxxxxx> wrote:
> 
> array_pluck is in the language, called array_column

array_pluck is designed to support dot expression, for example: 

array_pluck($array, "parent.child.child1.attr");

> array_remove is just array_filter, why do I need another function for that?

array_filter only passes value to the callback. in PHP 5.6, there is a new option to pass both key and value,
however PHP 5.5 doesn't have this option.

Implementing this function in extension make it possible.

> array_add - why do I need the overhead of a function call, when I can do $a[
> *key*] = *value*

The reason has been described in the document:  array_add only adds data when the key doesn't exist.

You can treat it as a set operation.

$array = [  ];
array_add($array, "key", 10);   // key => 10, return true
array_add($array, "key", 20);   // key => 10 still, return false

> array_build - this seems neat, but I think it needs a better example, I can
> do the example already with array_combine(array_column(), array_column())

By using array_build, you can pass complex structure as the new value, or adding complex logic in the callback.

> array_each - this is just a foreach with an isolated scope and function
> overhead on every iteration, why would I want that?

Yeah I have to admit it's pretty much similar, however sometimes you want to pass a iteration handler from external,
and array_map returns a new array, array_each is faster than array_map because it's much lightweight and it doesn't return value.

> array_keys_join - while I like this, why doesn't it have the same argument
> order as implode/join (glue, array)?

The reason I put array as the first argument is, consistency.  and actually the "glue" is optional, so you can simply ignore the argument.

$key = array_keys_join($array);
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php