There are many solutions to this, but here is an example of one of them:
Code: Select all
<?php
// prefix each value in the array with this
$prefix = 'Before ';
// input array
$a_name = array('james','mike','keith','dan');
// Map an anonymous function across each value in the array which prefixes each value
$a_name =& array_map(create_function('$arr_val', 'return '''.$prefix.'''.$arr_val;'), $a_name);
// If you don't want to create a copy, then use this to manipulate the values
// referenced in the array
array_walk($a_name, create_function('&$arr_val', '$arr_val = '''.$prefix.'''.$arr_val;'));
?>
Hopefully it's obvious, but only use 1 of array_map() or array_walk(). Take your pick.