array_map with str_replace array_map with str_replace php php

array_map with str_replace


There is no need for array_map. From the docs: "If subject is an array, then the search and replace is performed with every entry of subject, and the return value is an array as well."


No, it's not possible. Though, if you are using PHP 5.3, you can do something like this:

$data = array('foo bar baz');$data = array_map(function($value) { return str_replace('bar', 'xxx', $value); }, $data);print_r($data);

Output:

Array(    [0] => foo xxx baz)


Sure it's possible, you just have to give array_map() the correct input for the callback function.

array_map(    'str_replace',            // callback function (str_replace)    array_fill(0, $num, ' '), // first argument    ($search)    array_fill(0, $num, '-'), // second argument   ($replace)    $myArr                    // third argument    ($subject));

But for the particular example in the question, as chiborg said, there is no need. str_replace() will happily work on an array of strings.

str_replace(' ', '-', $myArr);