在PHP中更改多维数组的特定键

时间:2022-05-02 01:33:53

I have an two dimensional array called $array. After another question here, I know how to change the whole child array from a two dimensional array like this:

我有一个叫做$array的二维数组。在另一个问题之后,我知道如何从这样的二维数组中改变整个子数组:

Code

代码

$keys = array(0, 1);    
  foreach ($keys as $key) { 
      foreach ($array[$key] as $key2 => $value) {  
          $array[$key][$key2] = str_replace($stamm, $stamm2, $value);
        }
    } 

How is it possible to change only $array[0][3],$array[0][4] and $array[1]?

怎么可能只改变$array[0][3],$array[0][4]和$array[1]?

$array use $stamm + ending of an verb for a time for example "ons". I have to change for some cases the root of the verb ($stamm).
With the code above it works perfectly to change the whole array[0] and array[1]. .

$array使用$stamm +动词结尾,例如“ons”。在某些情况下,我必须改变动词的词根($stamm)。上面的代码可以很好地改变整个数组[0]和数组[1]。

1 个解决方案

#1


5  

Why dont you simply do this

你为什么不干脆这样做呢

$array[0][3] = //your value;
$array[0][4] =  //your value;  

and if you want to change all value of $array[1]:

如果你想改变$array[1]的所有值:

foreach ($array[1] as $key => $value) {  
      $array[1][$key] = //your value;
    }  

EDIT to do all this with a foreach :

编辑完成所有这些与一个foreach:

foreach ($keys as $key) { 
  foreach ($array[$key] as $key2 => $value) { 
      if(($key==0 && ($key2 == 3 || $key2 == 4)) || ($key == 1) ) 
          $array[$key][$key2] = //your value;
    }
} 

#1


5  

Why dont you simply do this

你为什么不干脆这样做呢

$array[0][3] = //your value;
$array[0][4] =  //your value;  

and if you want to change all value of $array[1]:

如果你想改变$array[1]的所有值:

foreach ($array[1] as $key => $value) {  
      $array[1][$key] = //your value;
    }  

EDIT to do all this with a foreach :

编辑完成所有这些与一个foreach:

foreach ($keys as $key) { 
  foreach ($array[$key] as $key2 => $value) { 
      if(($key==0 && ($key2 == 3 || $key2 == 4)) || ($key == 1) ) 
          $array[$key][$key2] = //your value;
    }
}