php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #31659 array_map to take non-array parameters and pass them to the callback function
Submitted: 2005-01-22 22:08 UTC Modified: 2014-12-31 15:52 UTC
Votes:5
Avg. Score:3.8 ± 0.4
Reproduced:5 of 5 (100.0%)
Same Version:0 (0.0%)
Same OS:2 (40.0%)
From: zizka at seznam dot cz Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 5.0.1 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
17 - 10 = ?
Subscribe to this entry?

 
 [2005-01-22 22:08 UTC] 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 ...

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-03-01 19:45 UTC] 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."
 [2014-12-31 15:52 UTC] nikic@php.net
-Status: Open +Status: Wont fix -Package: Feature/Change Request +Package: *General Issues
 [2014-12-31 15:52 UTC] nikic@php.net
This use-case should be covered by anonymous functions (since PHP 5.3).
 [2015-10-07 02:40 UTC] lucas at threeam dot com dot au
Anonymous functions don't obviate the usefulness of scalar arguments to array_map (from the 3rd argument onwards, anyway).

To work around this, use array_fill(). Using the previous example, this would be:

$a = array_map('str_replace', $a, array_fill(0, count($a), 'X'), array_fill(0, count($a), 'Y'));
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 01:01:28 2024 UTC