php 更改 foreach 循环内的值不会更改正在迭代的数组中的值

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

Changing value inside foreach loop doesn't change value in the array being iterated over

phpforeach

提问by user1179295

Why does this yield this:

为什么会产生这个:

foreach( $store as $key => $value){
$value = $value.".txt.gz";
}

unset($value);

print_r ($store);

Array
(
[1] => 101Phones - Product Catalog TXT
[2] => 1-800-FLORALS - Product Catalog 1
)

I am trying to get 101Phones - Product Catalog TXT.txt.gz

我正在尝试获取 101Phones - 产品目录 TXT.txt.gz

Thoughts on whats going on?

关于发生了什么的想法?

EDIT: Alright I found the solution...my variables in my array had values I couldn't see...doing

编辑:好吧,我找到了解决方案......我的数组中的变量有我看不到的值......做

$output = preg_replace('/[^(\x20-\x7F)]*/','', $output);
echo($output);

Cleaned it up and made it work properly

清理它并使其正常工作

回答by Michel

The doc http://php.net/manual/en/control-structures.foreach.phpclearly states why you have a problem:

文档http://php.net/manual/en/control-structures.foreach.php清楚地说明了您遇到问题的原因:

"In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference."

“为了能够直接修改循环中的数组元素,在 $value 前面加上 &。在这种情况下,值将通过引用分配。”

<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
    $value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
unset($value); // break the reference with the last element
?>

Referencing $value is only possible if the iterated array can be referenced (i.e. if it is a variable). The following code won't work:

仅当迭代数组可以被引用(即如果它是一个变量)时,才可能引用 $value。以下代码将不起作用:

<?php
/** this won't work **/
foreach (array(1, 2, 3, 4) as &$value) {
    $value = $value * 2;
}
?>

回答by Andy

Try

尝试

foreach( $store as $key => $value){
    $store[$key] = $value.".txt.gz";
}

回答by deceze

The $valuevariable in the array is temporary, it does not refer to the entry in the array.
If you want to change the original array entry, use a reference:

$value数组中的变量是临时的,它不引用数组中的条目。
如果要更改原始数组条目,请使用引用:

foreach ($store as $key => &$value) {
                       //  ^ reference
    $value .= '.txt.gz';
}

回答by Daniel

You are rewriting the value within the loop, and not the key reference in your array.

您正在重写循环中的值,而不是数组中的键引用。

Try

尝试

 $store[$key] = $value.".txt.gz";

回答by k102

pass $valueas a reference:

$value作为参考传递:

foreach( $store as $key => &$value){
   $value = $value.".txt.gz";
}

回答by Baba

Try

尝试

$catalog = array();

foreach( $store as $key => $value){
    $catalog[] = $value.".txt.gz";
}


print_r ($catalog);

OR

或者

foreach( $store as $key => $value){
    $store[$key] = $value.".txt.gz";
}


print_r ($store);

Depends on what you want to achieve

取决于你想要达到的目标

Thanks :)

谢谢 :)

回答by Chibueze Opata

I believe this is what you want to do:

我相信这是你想要做的:

foreach( $store as $key => $value){
$store[$key] = $value.".txt.gz";
}

unset($value);

print_r ($store);

回答by Ankush Jetly

foreach(array_container as & array_value)

Is the way to modify array element value inside foreach loop.

是在 foreach 循环中修改数组元素值的方法。

回答by Jaros?aw Gomu?ka

How about array map:

数组映射如何:

$func = function($value) { return $value . ".txt.gz"; };
print_r(array_map($func, $store));