用 PHP 替换数组中的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4977048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:24:58  来源:igfitidea点击:

Replace string in an array with PHP

phparraysstr-replacereplace

提问by hd.

How can I replace a sub string with some other string for all items of an array in PHP?

如何用其他字符串替换 PHP 中数组的所有项目的子字符串?

I don't want to use a loop to do it. Is there a predefined function in PHP that does exactly that?

我不想使用循环来做到这一点。PHP 中是否有一个预定义的函数可以做到这一点?

How can I do that on keys of array?

我怎样才能在数组的键上做到这一点?

回答by netcoder

Why not just use str_replacewithout a loop?

为什么不直接使用str_replace而不使用循环?

$array = array('foobar', 'foobaz');
$out = str_replace('foo', 'hello', $array);

回答by NikiC

$array = array_map(
    function($str) {
        return str_replace('foo', 'bar', $str);
    },
    $array
);

But array_mapis just a hidden loop. Why not use a real one?

但这array_map只是一个隐藏的循环。为什么不使用真机呢?

foreach ($array as &$str) {
    $str = str_replace('foo', 'bar', $str);
}

That's much easier.

这要容易得多。

回答by Impression eStudio

This is a very good idea that I found and used successfully:

这是我发现并成功使用的一个很好的想法:

function str_replace_json($search, $replace, $subject) 
{
    return json_decode(str_replace($search, $replace, json_encode($subject)), true);
}

It is good also for multidimensional arrays.

它也适用于多维数组。

If you change the "true" to "false" then it will return an object instead of an associative array.

如果将“true”更改为“false”,则它将返回一个对象而不是关联数组。

Source: Codelinks

来源:代码链接

回答by Golu

I am not sure how efficient this is, but I wanted to replace strings in a big multidimensional array and did not want to loop through all items as the array structure is pretty dynamic.

我不确定这有多有效,但我想替换一个大的多维数组中的字符串,并且不想遍历所有项目,因为数组结构非常动态。

I first json_encodethe array into a string.

我先把json_encode数组变成字符串。

Replace all the strings I want (need to use preg_replaceif there are non-English characters that get encoded by json_encode).

替换我想要的所有字符串(preg_replace如果有由 编码的非英文字符,则需要使用json_encode)。

json_decodeto get the array back.

json_decode取回数组。

回答by Ferhat KO?ER

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;
}

回答by J.BizMai

With array_walk_recursive()

array_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 ]
    );
}