array_map and append string to elements of an array

hd. picture hd. · Jan 14, 2012 · Viewed 8.5k times · Source

I have an array like this:

$a = array('aa', 'bb', 'cc', 'dd');

I want to add the 'rq' string at the beginning of all the elements of the array. Is it possible to do it by calling array_map() on this array?

Answer

deceze picture deceze · Jan 14, 2012
$a = array_map(function ($str) { return "rq$str"; }, $a);