array_map and add a string to the elements of the array

I have an array like this:

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

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

+4
source share
3 answers
 $a = array_map(function ($str) { return "rq$str"; }, $a); 
+10
source

You can have it like this:

 <?php $a = array('aa', 'bb', 'cc', 'dd'); $i=0; foreach($a as $d) { $a[$i] = 'rq'.$d; $i++; } var_dump($a); ?> 
+3
source
 function addRq($sValue) { return 'rq'.$sValue; } $newA = array_map("addRq", $a); 

Also see this example .

+3
source

Source: https://habr.com/ru/post/1390964/


All Articles