note 80571 added to function.array-map
| From: | pmf at osu1 dot php dot net | Date: | Wed, 23 Jan 2008 03:02:13 +0000 |
| Subject: | note 80571 added to function.array-map | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-135324@lists.php.net to get a copy of this message | ||
This function behaves exactly like array_map but additionally does not reject non-array arguments.
Instead, it transforms them with the array_fill function to a constant valued array of required
length according to the other array arguments (if any) and executes the original array_map function.
<?php
function array_map2() {
$args = func_get_args();
$callback = array_shift($args);
$args = array_map(
create_function('$a,$max','return is_array($a)? $a:
array_fill(0,$max,$a);'),
$args,array_fill(0,count($args),array_reduce($args,
create_function('$v,$w','return max($v,is_array($w)? count($w):
1);'))));
array_unshift($args,$callback);
return call_user_func_array("array_map",$args);
}
?>
Example:
<?php
$get = "first=value1&second=value2&third=value3";
print_r(array_map2("explode","=",explode("&",$get)));
?>
would print out:
<?php
Array
(
[0] => Array
(
[0] => first
[1] => value1
)
[1] => Array
(
[0] => second
[1] => value2
)
[2] => Array
(
[0] => third
[1] => value3
)
)
?>
/pmf
----
Server IP: 217.160.72.57
Probable Submitter: 80.171.45.65
----
Manual Page -- http://www.php.net/manual/en/function.array-map.php
Edit -- https://master.php.net/note/edit/80571
Del: integrated -- https://master.php.net/note/delete/80571/integrated
Del: useless -- https://master.php.net/note/delete/80571/useless
Del: bad code -- https://master.php.net/note/delete/80571/bad+code
Del: spam -- https://master.php.net/note/delete/80571/spam
Del: non-english -- https://master.php.net/note/delete/80571/non-english
Del: in docs -- https://master.php.net/note/delete/80571/in+docs
Del: other reasons-- https://master.php.net/note/delete/80571
Reject -- https://master.php.net/note/reject/80571
Search -- https://master.php.net/manage/user-notes.php