这是我的数组:
['apple']['some code']
['beta']['other code']
['cat']['other code 2 ']
我怎样才能用“!”替换所有的“e”字母?在键名中并保留值 这样我就会得到类似的东西
['appl!']['some code']
['b!ta']['other code']
['cat']['other code 2 ']
我找到了这个,但是因为我没有为所有键设置相同的名称,所以我不能使用它
$tags = array_map(function($tag) {
return array(
'name' => $tag['name'],
'value' => $tag['url']
);
}, $tags);
最佳答案
我希望你的数组看起来像这样:-
Array
(
[apple] => some code
[beta] => other code
[cat] => other code 2
)
如果是,那么你可以像下面那样做:-
$next_array = array();
foreach ($array as $key=>$val){
$next_array[str_replace('e','!',$key)] = $val;
}
echo "<pre/>";print_r($next_array);
关于php - 替换 php 数组中的所有键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43541389/