在foreach循环中更新数组值

时间:2022-10-26 10:55:27

I have this piece of code :

我有这段代码:

$start = ['23','', 'what'];
foreach($start as $i){
  if($i ==''){
    $i = 'satisfaction';
  }
}
print_r($start);

The output is :

输出是:

Array
(
[0] => 23
[1] => 
[2] => what
)

Why did index [1] not get replaced with 'satisfaction'. In other words : I don't want to create a new array, but change the index of an existing array. Actually, what I'm trying to achieve is to do intval() on those indexes that are not empty (since intval on an empty index returns 0, which is not what I want).

为什么索引[1]没有被'满意'取代。换句话说:我不想创建新数组,而是更改现有数组的索引。实际上,我想要实现的是对那些非空的索引执行intval()(因为空索引上的intval返回0,这不是我想要的)。

3 个解决方案

#1


According to the manual:

根据手册:

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之前加上&。在这种情况下,该值将通过引用分配。

So in your case, you should add &:

所以在你的情况下,你应该添加&:

$start = ['23','', 'what'];
foreach($start as &$i){
               // ^ reference
    if($i === ''){
        $i = 'satisfaction';
    }
}

Sidenote: If your intent is to change those numeric values into data type integer, you can use (as you stated) intval or a simple type cast.

旁注:如果您的目的是将这些数值更改为数据类型整数,则可以使用(如您所述)intval或简单类型转换。

$start = ['23','', 'what'];
foreach($start as &$i){
    if(is_numeric($i)){
        $i = (int) $i;
    }
}

var_dump($start);

#2


Because foreach(...) acts "a bit" as a read-only iterator. If you want to modify elements, you'll have to access by reference.

因为foreach(...)作为只读迭代器“有点”。如果要修改元素,则必须通过引用访问。

example :

foreach ($start as &$i) {

}

For more info, see doc : http://php.net/manual/fr/control-structures.foreach.php

有关详细信息,请参阅doc:http://php.net/manual/fr/control-structures.foreach.php

#3


In your example you are just setting the variable $i, which is just a temporary variable for the loop. Instead, keep the array key in the loop and use that to set the value in the array:

在您的示例中,您只是设置变量$ i,它只是循环的临时变量。相反,将数组键保留在循环中并使用它来设置数组中的值:

$start = ['23','', 'what'];
foreach($start as $k=>$i){
  if($i ==''){
     $start[$k] = 'satisfaction';
  }
}

#1


According to the manual:

根据手册:

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之前加上&。在这种情况下,该值将通过引用分配。

So in your case, you should add &:

所以在你的情况下,你应该添加&:

$start = ['23','', 'what'];
foreach($start as &$i){
               // ^ reference
    if($i === ''){
        $i = 'satisfaction';
    }
}

Sidenote: If your intent is to change those numeric values into data type integer, you can use (as you stated) intval or a simple type cast.

旁注:如果您的目的是将这些数值更改为数据类型整数,则可以使用(如您所述)intval或简单类型转换。

$start = ['23','', 'what'];
foreach($start as &$i){
    if(is_numeric($i)){
        $i = (int) $i;
    }
}

var_dump($start);

#2


Because foreach(...) acts "a bit" as a read-only iterator. If you want to modify elements, you'll have to access by reference.

因为foreach(...)作为只读迭代器“有点”。如果要修改元素,则必须通过引用访问。

example :

foreach ($start as &$i) {

}

For more info, see doc : http://php.net/manual/fr/control-structures.foreach.php

有关详细信息,请参阅doc:http://php.net/manual/fr/control-structures.foreach.php

#3


In your example you are just setting the variable $i, which is just a temporary variable for the loop. Instead, keep the array key in the loop and use that to set the value in the array:

在您的示例中,您只是设置变量$ i,它只是循环的临时变量。相反,将数组键保留在循环中并使用它来设置数组中的值:

$start = ['23','', 'what'];
foreach($start as $k=>$i){
  if($i ==''){
     $start[$k] = 'satisfaction';
  }
}