替换php数组中的所有键 [英] replace all keys in php array

查看:53
本文介绍了替换php数组中的所有键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的数组:

['apple']['some code'] 
['beta']['other code']
['cat']['other code 2 ']

如何将所有"e"字母替换为!"在键名称中并保留值这样我会得到类似的东西

how can I replace all the "e" letters with "!" in the key name and keep the values so that I will get something like that

['appl!']['some code'] 
['b!ta']['other code']
['cat']['other code 2 ']

我找到了它,但是因为我所有的键名都不一样,所以我不能使用它

I found this but because I don't have the same name for all keys I can't use It

$tags = array_map(function($tag) {
    return array(
        'name' => $tag['name'],
        'value' => $tag['url']
    );
}, $tags);

推荐答案

我希望您的数组如下所示:-

I hope your array looks like this:-

Array
(
    [apple] => some code
    [beta] => other code
    [cat] => other code 2 
)

如果是,那么您可以按照以下步骤进行操作:-

If yes then you can do it like below:-

$next_array = array();
foreach ($array as $key=>$val){
     $next_array[str_replace('e','!',$key)] = $val;
}
echo "<pre/>";print_r($next_array);

输出:- https://eval.in/780144

这篇关于替换php数组中的所有键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆