Skip to content
Advertisement

Modify array during multiple foreach loop iterations

I’ve to update data for array which has 4 foreach loops,

     foreach ($dta['hotels']['hotels'] as $key => &$value) {
      foreach ($value['rooms'] as $key1 => $value1) {
        foreach ($value1['rates'] as $key2 => $value2) {
          foreach ($value2['shiftRates'] as $key3 => &$value3) {
            $value3['net'] = 0.000072*$value3['net'];
            $value3['sellingRate'] = 0.000072*$value3['sellingRate'];
            var_dump($value3['sellingRate']);
          }
        }
      }
      $value['currency'] = 'USD';
    }

I want to update data of very deep 4th foreach loop, which isn’t updating data, where as first loop data update was possible.

i’ve tried to put “&” but in first loop it worked and in 4th loop it’s not working.

Any possible solutions ?

Advertisement

Answer

You have all keys, you can use these to modify your values :

$dta['hotels']['hotels'][$key]['rooms'][$key1]['rates'][$key2]['shiftRates'][$key3]['sellingRate'] = 0.000072 * $value3['sellingRate'];
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement