# array_map() function modification

- Thread: https://externals.io/message/64898
- Original archive: https://news-web.php.net/php.internals/64898
- Started by Thomas Hruska on 2013-01-12T17:34:26+00:00
- Messages: 8
- 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

---

## #64898 — Thomas Hruska — 2013-01-12T17:34:26+00:00

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

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.

http://cubiclesoft.com/

---

## #64900 — Alexey Zakhlestin — 2013-01-12T17:46:34+00:00

URL: https://externals.io/message/64898#64900 · In reply to: #64898

> 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

---

## #64901 — Clint Priest — 2013-01-12T17:58:09+00:00

URL: https://externals.io/message/64898#64901 · In reply to: #64900

> There is a function for this: http://docs.php.net/array_combine 
Nobody ever knows that one, I use it frequently, so useful.

-- 
-Clint

---

## #64903 — Thomas Hruska — 2013-01-12T19:41:55+00:00

URL: https://externals.io/message/64898#64903 · In reply to: #64900

>
> 
>
> > […]
>
> There is a function for this:
>
> http://docs.php.net/array_combine


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.

http://cubiclesoft.com/

---

## #64905 — Galen Wright-Watson — 2013-01-12T20:06:15+00:00

URL: https://externals.io/message/64898#64905 · In reply to: #64903

On Sat, Jan 12, 2013 at 11:41 AM, Thomas Hruska <thruska@cubiclesoft.com>wrote:

> [...]
>
> 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 in `array_map()`, then `array_combine()` could call `array_map()` and
> could potentially just be an alias to `array_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 how `array_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?

---

## #64906 — Peter Cowburn — 2013-01-12T20:10:39+00:00

URL: https://externals.io/message/64898#64906 · In reply to: #64905

>
> 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));

---

## #64907 — Pierre du Plessis — 2013-01-12T20:12:54+00:00

URL: https://externals.io/message/64898#64907 · In reply to: #64903

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.com>wrote:

> 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

---

## #64910 — Thomas Hruska — 2013-01-12T20:38:17+00:00

URL: https://externals.io/message/64898#64910 · In reply to: #64906

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

http://cubiclesoft.com/
