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/

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

Reply via email to