PHP: 数组成员批量添加前缀, Adding prefix strings to array values

问题:

$myarray = array("test", "test2", "test3");   
#转换为
$myarray = array("prefix_test", "prefix_test2", "prefix_test3");

 

方法一:

$myarray = array("test", "test2", "test3"); 
array_walk($myarray, function(&$value, $key) { $value = 'prefix_'.$value; } );

方法二:

$myarray = array("test", "test2", "test3");
$array = array_map(function($value) { return ' prefix_'.$value; }, $myarray);

方法三:

$myarray = array("test", "test2", "test3");    
$prefixed_array = preg_filter('/^/', 'prefix_', $myarray);

 

 

本文:PHP: 数组成员批量添加前缀, Adding prefix strings to array values

Leave a Reply