6

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?

1
  • You mean 'prepend'... Commented May 14, 2018 at 18:46

3 Answers 3

14
$a = array_map(function ($str) { return "rq$str"; }, $a);
Sign up to request clarification or add additional context in comments.

Comments

3
function addRq($sValue) {
    return 'rq'.$sValue;
}
$newA = array_map("addRq", $a);

Also see this example.

Comments

2

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);
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.