Replace string in an array with PHP

hd. picture hd. · Feb 12, 2011 · Viewed 53.7k times · Source

How can I replace a sub string with some other string for all items of an array in PHP?

I don't want to use a loop to do it. Is there a predefined function in PHP that does exactly that?

How can I do that on keys of array?

Answer

netcoder picture netcoder · Feb 12, 2011

Why not just use str_replace without a loop?

$array = array('foobar', 'foobaz');
$out = str_replace('foo', 'hello', $array);