1

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 ?

0

2 Answers 2

2

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'];
Sign up to request clarification or add additional context in comments.

Comments

0

As long as there are no other net or sellingRate keys somewhere else in the array that you don't want to modify, you can do this more simply with array_walk_recursive.

array_walk_recursive($dta, function(&$value, $key) {
    if ($key === 'net' || $key === 'sellingRate') {
        $value *= 0.000072;
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.