Req #31659 [Opn->Wfx]: array_map to take non-array parameters and pass them to the callback function
| From: | nikic@php.net | Date: | Wed, 31 Dec 2014 15:52:45 +0000 |
| Subject: | Req #31659 [Opn->Wfx]: array_map to take non-array parameters and pass them to the callback function | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-189561@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=31659&edit=1
ID: 31659
Updated by: nikic@php.net
Reported by: zizka at seznam dot cz
Summary: array_map to take non-array parameters and pass them
to the callback function
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
-Package: Feature/Change Request
+Package: *General Issues
PHP Version: 5.0.1
Block user comment: N
Private report: N
New Comment:
This use-case should be covered by anonymous functions (since PHP 5.3).
Previous Comments:
------------------------------------------------------------------------
[2005-03-01 19:45:23] zizka at seznam dot cz
Another solution is to pass arrays with several identical members:
$a = explode(' ', 'XyX aXb sXs');
$a = array_map('str_replace', Array('X','X','X'),
Array('Y','Y','Y'), $a);
But the arrays are superfluous in this case, as a scalar value would be enough. And with this case
there is a problem that the supplementary arrays must be of at least the same length as $a:
PHP Manual: "Usually when using two or more arrays, they should be of equal length because the
callback function is applied in parallel to the corresponding elements. If the arrays are of unequal
length, the shortest one will be extended with empty elements."
------------------------------------------------------------------------
[2005-01-22 22:08:44] zizka at seznam dot cz
Description:
------------
array_map could take non-array parameters and pass them to the callback function in each step.
Example:
$a = explode(' ', 'XyX aXb sXs');
$a = array_map('str_replace', 'X','Y', $a);
That would result in calling:
str_replace('X','Y', $a[...]);
and the result array would be like
Array('YyY', 'aYb', 'sYs');
Now I have to define a callback function for many simple operations that could be done this way.
Thanks, Ondra Zizka
Reproduce code:
---------------
$a = explode(' ', 'XyX aXb sXs');
$a = array_map('str_replace', 'X','Y', $a);
print_r($a);
Expected result:
----------------
Array( [0] => YyY [1] => aYb [2] => sYs )
Actual result:
--------------
Warning: array_map() [function.array-map.htm]: Argument #2 should be an array in ...
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=31659&edit=1