在使用PHP数组替换字符串 [英] replace string in an array with php
本文介绍了在使用PHP数组替换字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何替换子字符串与其他一些字符串PHP中的数组的所有项目?
我不想用一个循环做到这一点。有没有在PHP predefined函数正是这么做的?
编辑:谢谢您的回复,还有一个问题,我该怎么办,关于按键排列
解决方案
$阵列= array_map(
功能($ STR){
str_replace函数返回('富','酒吧',$海峡);
},
$阵列
);
但 array_map
只是一个隐藏的循环。为什么不使用真实的?
的foreach($数组和放大器; $ STR){
$海峡= str_replace函数('富','酒吧',$海峡);
}
这更容易。
How can I replace a sub string with some other string for all items of an array in php? I don't want to use a loop to do it. Is there a predefined function in php that does exactly that?
Edited: Thank you for your reply,one more question,how can i do that on keys of array?
解决方案
$array = array_map(
function($str) {
return str_replace('foo', 'bar', $str);
},
$array
);
But array_map
is just a hidden loop. Why not use a real one?
foreach ($array as &$str) {
$str = str_replace('foo', 'bar', $str);
}
That's much easier.
这篇关于在使用PHP数组替换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文