如何在 PHP 中将一个子字符串替换为某个其他字符串,以对数组中所有项目进行操作?
我不想使用循环来完成。 PHP 中是否有预定义函数可以完全实现这一点?
如何在数组键上执行此操作?
我不想使用循环来完成。 PHP 中是否有预定义函数可以完全实现这一点?
如何在数组键上执行此操作?
为什么不直接使用 str_replace 函数而不需要循环?
$array = array('foobar', 'foobaz');
$out = str_replace('foo', 'hello', $array);
PHP Catchable fatal error: Object of class stdClass could not be converted to string
。我对PHP非常陌生,到目前为止编辑数组似乎很困难。 - Dennisjson_decode
默认返回一个对象,而不是一个数组,除非你明确指定要一个数组。 - netcoder$array = array_map(
function($str) {
return str_replace('foo', 'bar', $str);
},
$array
);
但是array_map
只是一个隐藏的循环。为什么不使用一个真正的循环呢?
foreach ($array as &$str) {
$str = str_replace('foo', 'bar', $str);
}
那样容易多了。
我发现并成功使用了一个非常好的想法:
function str_replace_json($search, $replace, $subject)
{
return json_decode(str_replace($search, $replace, json_encode($subject)), true);
}
它对于多维数组也非常好用。
如果您将“true”更改为“false”,它将返回一个对象而不是关联数组。
来源:Codelinks
{}
,那么它将无法工作。 - Max S.function my_replace_array($array,$key,$val){
for($i=0;$i<count($array);$i++){
if(is_array($array[$i])){
$array[$i] = my_replace_array($array[$i],$key,$val);
}else{
$array[$i]=str_replace($key,$val,$array[$i]);
}
}
return $array;
}
我不确定这种方法的效率如何,但我想替换一个大的多维数组中的字符串,而且不想循环遍历所有项,因为数组结构非常动态。
我首先将该数组使用json_encode
编码成字符串。
替换所有我想要的字符串(如果有非英文字符被json_encode
编码,则需要使用preg_replace
)。
json_decode
以获取数组。
str_replace()
或 preg_replace()
会使所有键、值和 json 格式字符易受变异的影响。可能存在一些情况下这种技术是安全的,但这种通用建议不应该被实施为一般性的方法。 - mickmackusaarray_walk_recursive()
函数。function replace_array_recursive( string $needle, string $replace, array &$haystack ){
array_walk_recursive($haystack,
function (&$item, $key, $data){
$item = str_replace( $data['needle'], $data['replace'], $item );
return $item;
},
[ 'needle' => $needle, 'replace' => $replace ]
);
}
return $item;
是否真的必要? - mickmackusa自 PHP 8.1 开始,不再有预定义的函数。因此,这将替换所有元素,即使存在对象:
<?php
class Arrays {
/**
* Replaces in all strings within a multidimensional array, even if objects are included
* @param string $search search for this string
* @param string $replace_with replace with this string
* @param mixed $haystack Array or Object to search in all elements
* @return mixed gives the same structure back
*/
public static function replace_everywhere($search, $replace_with, $haystack) {
if (is_array($haystack) or is_object($haystack)) {
// Arrays and Objects
foreach ($haystack as &$value) {
$value = Arrays::replace_everywhere($search, $replace_with, $value);
}
return $haystack;
} elseif (is_string($haystack)) {
// replace in a string element
return str_replace($search, $replace_with, $haystack);
} else {
// other datatypes (e.g. integer) stay untouched
return $haystack;
}
}
}
// You can call this like e.g.
$a = array(true, 1, 'foo<bar', 'foo<baz', array("foo<loo"), (object) array('1' => 'foo<boo'));
$a = Arrays::replace_everywhere("<", "<", $a);
var_export($a);
示例:https://3v4l.org/CDGvC#v8.2.5
如果你想要替换数组键,我认为这是另一个问题,有很好的答案,比如:递归更改数组键
str_replace()
的范围。换句话说,你的答案更适合回答不同的问题。 - mickmackusa$base = array('citrus' => array( "orange") , 'berries' => array("blackberry", "raspberry"), );
$replacements = array('citrus' => array('pineapple'), 'berries' => array('blueberry'));
$basket = array_replace_recursive($base, $replacements);
$basket = array_replace($base, $replacements);
str_replace
已经可以做到这一点。 - Gavin