字符串替换数组 PHP 中的所有项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5045101/
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:38:09  来源:igfitidea点击:

String replace all items in array PHP

phpstringreplaceforeachstr-replace

提问by AJFMEDIA

I would like to do a string replacement in all items in an array. What I have is:

我想在数组中的所有项目中进行字符串替换。我所拥有的是:

$row['c1'] = str_replace("&", "&", $row['c1']);
$row['c2'] = str_replace("&", "&", $row['c2']);
$row['c3'] = str_replace("&", "&", $row['c3']);
$row['c4'] = str_replace("&", "&", $row['c4']);   
$row['c5'] = str_replace("&", "&", $row['c5']);
$row['c6'] = str_replace("&", "&", $row['c6']);
$row['c7'] = str_replace("&", "&", $row['c7']);   
$row['c8'] = str_replace("&", "&", $row['c8']);
$row['c9'] = str_replace("&", "&", $row['c9']);
$row['c10'] = str_replace("&", "&", $row['c10']); 

How can I achieve this with less code? I thought a foreach statement would work, e.g.:

如何用更少的代码实现这一目标?我认为 foreach 语句会起作用,例如:

$columns = array($row['c1'], $row['c2'], $row['c3'], $row['c4'], $row['c5'], $row['c6'], $row['c7'], $row['c8'], $row['c9'], $row['c10']);

foreach ( $columns as $value){
   $value   = str_replace("&", "&", $value);
}

But it doesn't work.

但它不起作用。

回答by netcoder

Just do:

做就是了:

$row = str_replace("&", "&", $row);

Note:Your foreach doesn't work because you need a reference, or use the key:

注意:您的 foreach 不起作用,因为您需要引用或使用密钥:

foreach ( $columns as &$value) { // reference
   $value  = str_replace("&", "&", $value);
}
unset($value); // break the reference with the last element

Or:

或者:

foreach ($columns as $key => $value){
   $columns[$key]  = str_replace("&", "&", $value);
}

Although it is not necessary here because str_replaceaccepts and returns arrays.

虽然这里没有必要,因为str_replace接受并返回数组。

回答by Ish

You should call it by reference, otherwise foreachcreates a duplicate copy of $value

您应该通过引用调用它,否则foreach会创建一个重复的副本$value

foreach ( $columns as &$value)

foreach ( $columns as &$value)

回答by mr.baby123

Another solution to is to use PHP array_walklike this:

另一个解决方案是array_walk像这样使用 PHP :

function custom_replace( &$item, $key ) {
   $item = str_replace('22', '75', $item);
} 

// Init dummy array.
$columns = array('Cabbage22', 'Frid22ay', 'Internet', 'Place22', '22Salary', '22Stretch', 'Whale22Inn');

// Print BEFORE.
echo 'Before: ';
print_r($columns);

// Make the replacements.
array_walk($columns, 'custom_replace');

// Print AFTER.
echo 'After:';
print_r($columns);

Output:

输出:

Before: Array
(
    [0] => Cabbage22
    [1] => Frid22ay
    [2] => Internet
    [3] => Place22
    [4] => 22Salary
    [5] => 22Stretch
    [6] => Whale22Inn
)
After: Array
(
    [0] => Cabbage75
    [1] => Frid75ay
    [2] => Internet
    [3] => Place75
    [4] => 75Salary
    [5] => 75Stretch
    [6] => Whale75Inn
)