ID:              31659
 User updated by: zizka at seznam dot cz
 Reported By:     zizka at seznam dot cz
 Status:          Open
 Bug Type:        Feature/Change Request
 PHP Version:     5.0.1
 New Comment:

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."


Previous Comments:
------------------------------------------------------------------------

[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 http://bugs.php.net/?id=31659&edit=1

Reply via email to