While we are on the general thought of arrays on this list, I was
originally going to put in an idea for a new function and let it simmer
for a while. Then I remembered that array_map()
almost does what I
want, but not quite. Someone can start a RFC if they like this idea.
I thoroughly abuse PHP's highly performant ordered key-value map feature
(aka arrays). The fantastic performance of 'isset' is the primary
reason I enjoy using PHP.
One thing I find myself doing frequently, in order to feed my habit of
abusing PHP arrays, is construct key-value arrays from other arrays.
array_map()
comes close but can't quite handle it. So I wish to humbly
propose the following change to array_map()
:
array array_map ( mixed $callback_or_array , array $arr1 [, array $... ] )
If the first parameter is a 'callable' function or null, array_map()
acts exactly like it does right now. If the first parameter is an
array, it should be the same length as the other arrays and each value
within the array must be a string or integer to use as a key for the
value(s) in the subsequent parameters. The rules for keys are similar
to how array_flip()
works where duplicate keys result in the value being
overwritten in the resulting array. If the function runs out of keys or
values, it fills out the rest with integers or nulls respectively.
This would allow developers to do things like:
$keys = array('key1', 'key2', ..., 'keyn');
$vals = array('val1', 'val2', ..., 'valn');
$somemap = array_map($keys, $vals);
Which would result in $somemap containing:
array('key1' => 'val1', 'key2' => 'val2', ..., 'keyn' => 'valn')
In addition, passing multiple value arrays would do something naturally
in line with the rest of how array_map()
works now but with keys sourced
from the keys array.
Used in conjunction with array_column()
being discussed in a separate
thread, this could eliminate the need for array_column()
's third
optional parameter. While having the third parameter in array_column()
is still useful, this is a more general-purpose solution.
--
Thomas Hruska
CubicleSoft President
I've got great, time saving software that you might find useful.
This would allow developers to do things like:
$keys = array('key1', 'key2', ..., 'keyn');
$vals = array('val1', 'val2', ..., 'valn');$somemap = array_map($keys, $vals);
Which would result in $somemap containing:
array('key1' => 'val1', 'key2' => 'val2', ..., 'keyn' => 'valn')
There is a function for this:
http://docs.php.net/array_combine
--
Alexey Zakhlestin
https://github.com/indeyets
There is a function for this: http://docs.php.net/array_combine
Nobody ever knows that one, I use it frequently, so useful.
--
-Clint
This would allow developers to do things like:
$keys = array('key1', 'key2', ..., 'keyn');
$vals = array('val1', 'val2', ..., 'valn');$somemap = array_map($keys, $vals);
Which would result in $somemap containing:
array('key1' => 'val1', 'key2' => 'val2', ..., 'keyn' => 'valn')
There is a function for this:
Hmm...never knew about that one. It seems poorly named.
The array_map()
changes would allow for multiple arrays of values:
array_map($keys, $vals, $vals2, $vals3);
Which I suppose could be emulated with a combination of array_combine()
with array_map()
:
array_combine($keys, array_map(null, $vals, $vals2, $vals3));
But it would execute faster if it were supported in array_map()
. If it
is supported in array_map()
, then array_combine()
could call array_map()
and could potentially just be an alias to array_map()
.
--
Thomas Hruska
CubicleSoft President
I've got great, time saving software that you might find useful.
On Sat, Jan 12, 2013 at 11:41 AM, Thomas Hruska thruska@cubiclesoft.comwrote:
[...]
The
array_map()
changes would allow for multiple arrays of values:array_map($keys, $vals, $vals2, $vals3);
[...]
But it would execute faster if it were supported in
array_map()
. If it is
supported inarray_map()
, thenarray_combine()
could callarray_map()
and
could potentially just be an alias toarray_map()
.
Alternatively, array_combine()
could be made variadic, adding your proposed
behavior (which is more in line with the current behavior of
array_combine()
than array_map()
).
Speaking of,
On Sat, Jan 12, 2013 at 9:34 AM, Thomas Hruska thruska@cubiclesoft.com
wrote:
[...]
In addition, passing multiple value arrays would do something naturally in
line with the rest of howarray_map()
works now but with keys sourced from
the keys array.
Just to be clear, do you mean the result would be:
array($keys[0] => array($vals0[0], $vals1[0], ...),
$keys[1] => array($vals0[1], $vals1[1], ...),
...)
so the i-th item in the result would be an array with the i-th items of
each value array, and the i-th item of the key array as the key?
Just to be clear, do you mean the result would be:
array($keys[0] => array($vals0[0], $vals1[0], ...),
$keys[1] => array($vals0[1], $vals1[1], ...),
...)so the i-th item in the result would be an array with the i-th items of
each value array, and the i-th item of the key array as the key?
I believe so, yes. That is currently how the following (an example
already given by Thomas) would work today:
array_combine($keys, array_map(null, $vals0, $vals1, $vals2));
Just to be clear, do you mean the result would be:
array($keys[0] => array($vals0[0], $vals1[0], ...),
$keys[1] => array($vals0[1], $vals1[1], ...),
...)so the i-th item in the result would be an array with the i-th items of
each value array, and the i-th item of the key array as the key?I believe so, yes. That is currently how the following (an example
already given by Thomas) would work today:array_combine($keys, array_map(null, $vals0, $vals1, $vals2));
That was indeed the intent.
--
Thomas Hruska
CubicleSoft President
I've got great, time saving software that you might find useful.
I'm not sure that adding this functionality in array_map would actually
execute faster than doing array_combine($keys, array_map(null, $vals,
$vals2, $vals3));. I will need to do some benchmarks to test, but I'm sure
you will only see a performance improvement with extremely large arrays.
Also why change a function to add functionality that can already be
achieved with another existing function? Anybody using array_map is already
aware of it's functionality and what it is used for. To change it to add
other use cases, which is already available with array_combine, will just
be confusing to most developers.
If there is a serious need for multiple arrays for values, then I'd rather
argue that array_combine be modified to accept multiple arrays, which won't
break BC, but I doubt that it would be feasible for the majority developers
to have this feature added, as it is very easily implement in user-land
with a few lines of code.
On Sat, Jan 12, 2013 at 9:41 PM, Thomas Hruska thruska@cubiclesoft.comwrote:
array_combine($keys, array_map(null, $vals, $vals2, $vals3));
Kind Regards
Pierre du Plessis
Cell: 072 775 3477
Fax: 086 650 4991
Email: info@customscripts.co.za admin@customscripts.co.za
www: http://www.customscripts.co.za