note 107690 added to function.array-map
| From: | gmail dot com at mspreij | Date: | Sun, 26 Feb 2012 22:48:21 +0000 |
| Subject: | note 107690 added to function.array-map | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186373@lists.php.net to get a copy of this message | ||
Hope I'm not late to the party, here's my function to apply array_map to the *keys* of an
array.
Extra array arguments will be used for the callback function's parameters just like with
array_map, with the difference that a string is also allowed: it will just be used to create an
array of appropriate length with as each value that string. Arrays are left alone (and will be
padded with nulls by array_map as needed).
<?php
//_________________________________________________
// array_map_keys($callback, $array, [$args, ..]) /
function array_map_keys($callback, $array /* [, $args ..] */) {
$args = func_get_args();
if (! is_callable($callback)) trigger_error("first argument (callback) is not a valid
function", E_USER_ERROR);
if (! is_array($array)) trigger_error("second argument must be an array", E_USER_ERROR);
$args[1] = array_keys($array);
// If any additional arguments are not arrays, assume that value is wanted for every $array item.
// array_map() will pad shorter arrays with Null values
for ($i=2; $i < count($args); $i++) {
if (! is_array($args[$i])) {
$args[$i] = array_fill(0, count($array), $args[$i]);
}
}
return array_combine(call_user_func_array('array_map', $args), $array);
}
// Some examples:
$arr = array('foo'=>123, 'bar'=>456);
// simply uppercase keys:
var_dump(array_map_keys('strtoupper', $arr));
// or..
var_dump(array_map_keys(function($input) {return strtoupper($input);}, $arr));
// >> array(2) { ["FOO"]=>int(123) , ["BAR"]=> int(456) }
// Add a prefix 'myvar_':
var_dump(array_map_keys(function($input, $prefix) {return $prefix.$input;}, $arr,
'myvar_'));
// >> array(2) { ["myvar_foo"]=>int(123) , ["myvar_bar"]=>int(456) }
// Apart from the (static string) prefix, we also number them:
$arr = array('foo'=>123, 'bar'=>456, 'bazz'=>789,
'yadda'=>'0AB');
var_dump(array_map_keys(function($input, $middle, $number) {return
$number.':'.$middle.$input;}, $arr, 'myvar_', range(1, count($arr))));
// >> array(4) { ["1:myvar_foo"]=>int(123) ,
["2:myvar_bar"]=>int(456) , ["3:myvar_bazz"]=>int(789) ,
["4:myvar_yadda"]=>string(3) "0AB" }
?>
----
Server IP: 83.137.20.135
Probable Submitter: 82.73.20.238
----
Manual Page -- http://www.php.net/manual/en/function.array-map.php
Edit -- https://master.php.net/note/edit/107690
Del: integrated -- https://master.php.net/note/delete/107690/integrated
Del: useless -- https://master.php.net/note/delete/107690/useless
Del: bad code -- https://master.php.net/note/delete/107690/bad+code
Del: spam -- https://master.php.net/note/delete/107690/spam
Del: non-english -- https://master.php.net/note/delete/107690/non-english
Del: in docs -- https://master.php.net/note/delete/107690/in+docs
Del: other reasons-- https://master.php.net/note/delete/107690
Reject -- https://master.php.net/note/reject/107690
Search -- https://master.php.net/manage/user-notes.php