PHP: 数组键添加前缀后缀, 数组值添加前缀后缀, Adding prefix strings to array values, add prefix in each key of PHP Array?

 

 

1. 修改数组键名:

$myArray = ['0'=>'Hi','1'=>'Hello','2'=>'Hey'];
$myNewArray = array_combine(
    array_map(function($key){ return 'a'.$key; }, array_keys($myArray)),
    $myArray
);
print_r($myNewArray);

 

输出:

Array
(
    [a0] => Hi
    [a1] => Hello
    [a2] => Hey
)

 

2. 修改数组值:

 

在每个值后面追加字符

$array = array('a', 'b', 'c');
array_walk($array, function(&$value, $key) { $value .= 'd'; } );

 

在每个值前面添加空格

$array = array('a', 'b', 'c');
$array = array_map(function($value) { return ' '.$value; }, $array);

 

PHP: 数组键添加前缀后缀, 数组值添加前缀后缀, Adding prefix strings to array values, add prefix in each key of PHP Array?
PHP: 数组键添加前缀后缀, 数组值添加前缀后缀, Adding prefix strings to array values, add prefix in each key of PHP Array?
PHP: 数组键添加前缀后缀, 数组值添加前缀后缀, Adding prefix strings to array values, add prefix in each key of PHP Array?
PHP: 数组键添加前缀后缀, 数组值添加前缀后缀, Adding prefix strings to array values, add prefix in each key of PHP Array?

 

PHP: 数组键添加前缀后缀,  数组值添加前缀后缀, Adding prefix strings to array values, add prefix in each key of PHP Array?
PHP: 数组键添加前缀后缀, 数组值添加前缀后缀, Adding prefix strings to array values, add prefix in each key of PHP Array?

 

本文: PHP: 数组键添加前缀后缀, 数组值添加前缀后缀, Adding prefix strings to array values, add prefix in each key of PHP Array?

Leave a Reply