简体   繁体   English

PHP循环数组和更新

[英]php loop array and update

I have an array and I wish to update the values in roomTotalPrice. 我有一个数组,我希望更新roomTotalPrice中的值。 However when I loop, it changes to just a variable. 但是,当我循环时,它变成一个变量。

Array I want to change: 我要更改的数组:

Array
(
    [10] => Array
        (
            [12] => Array
                (
                    [num_rooms] => 2
                    [adults] => Array
                        (
                            [0] => 2
                            [1] => 2
                        )

                    [prices] => Array
                        (
                            [0] => 44.5
                            [1] => 44.5
                        )

                    [roomTotalPrice] => Array
                        (
                            [0] => 44.5
                            [1] => 44.5
                        )

                    [price] => 178
                    [supp] => 0
                )

        )

Code I am using: 我正在使用的代码:

//Total Room Price
foreach($iroom['roomTotalPrice'] as $irt){
    $s_rate[$iraid][$iroid]['roomTotalPrice'] = 100;
}



Array
(
    [10] => Array
        (
            [12] => Array
                (
                    [num_rooms] => 2
                    [adults] => Array
                        (
                            [0] => 2
                            [1] => 2
                        )

                    [prices] => Array
                        (
                            [0] => 44.5
                            [1] => 44.5
                        )

                    [roomTotalPrice] => 100
                    [price] => 178
                    [supp] => 0
                )

        )

Use this code: 使用此代码:

foreach($iroom['roomTotalPrice'] as &$irt){
    $irt = 100;
}

Anyway this code is based on the fact that $iroom['roomTotalPrice'] loop on the right sub-array as you have written. 无论如何,此代码基于$ iroom ['roomTotalPrice']在您编写的正确子数组上循环的事实。

Are you trying to make the sum for prices ? 您是否要按价格求和?

$x[10][12][roomTotalPrice] = array_sum($x[10][12][roomTotalPrice]) $ x [10] [12] [roomTotalPrice] = array_sum($ x [10] [12] [roomTotalPrice])

Assuming that the $iroom variable is the array in your first code sample, I believe you can use the following code to set all 'roomTotalPrice' entries to 100: 假设$ iroom变​​量是您的第一个代码示例中的数组,我相信您可以使用以下代码将所有“ roomTotalPrice”条目设置为100:

foreach ($iroom as $firstLevelIndex => $firstLevelArray) {
    foreach ($firstLevelArray as $secondLevelIndex => $secondLevelArray) {
        $iroom[$firstLevelIndex][$secondLevelIndex]['roomTotalPrice'] = 100;
    }
}

Is this what your want? 这是你想要的吗?

foreach ($iroom as $k1 => $v1) { // Loop outer array
  foreach ($v1 as $k2 => $v2) if (isset($v2['roomTotalPrice'])) { // Loop inner arrays and make sure they have a roomTotalPrice key
    foreach ($v2['roomTotalPrice'] as $k3 => $v3) { // Loop roomTotalPrice array
      $iroom[$k1][$k2]['roomTotalPrice'][$k3] = 100; // Change the values
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM