php循环数组得到总数

时间:2011-10-14 00:00:35

标签: php arrays

我在循环数组和计算字段时遇到了一些困难。这是数组$ iroom:

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

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

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

   [price] => 178
)

我想(成人*价格)+(成人* $ asup)+(chidern * $ csup)+ $ ssup并将答案输入roomTotalPrice。到目前为止,外部的forloop设置了roomTotalPrice价格,但我无法获得内部循环来计算价格。 $ sup是额外的补充价格。

到目前为止我得到的代码:

                        foreach($iroom['roomTotalPrice'] as &$irt){
                            foreach($iroom['adults'] as $ira){

                            }
                            $irt = ;
                        }

1 个答案:

答案 0 :(得分:2)

功能包裹的代码,处理新的阵列格式

/*
  Note that this function may not be 100% correct. I notice you have removed
  the 'supp' key from the array, and that your current spec doesn't do anything
  with the 'price' key. I suspect you may want the line

    + ((isset($array['supp'])) ? $array['supp'] : 0);

  to read

    + ((isset($array['price'])) ? $array['price'] : 0);

*/    

function calculateTotalPrices ($array, $asup = 10, $csup = 10) {
  if (!is_array($array) || !isset($array['num_rooms']) || !$array['num_rooms']) return FALSE; // make sure data is valid
  for ($i = 0; $i < $array['num_rooms']; $i++) { // Loop num_rooms times
    $array['roomTotalPrice'][$i] =
      ((isset($array['adults'][$i],$array['prices'][$i])) ? ($array['adults'][$i] * $array['prices'][$i]) + ($array['adults'][$i] * $asup) : 0) // Calculate price for adults
      + ((isset($array['childern'][$i])) ? ($array['childern'][$i] * $csup) : 0) // Calculate price for children
      + ((isset($array['supp'])) ? $array['supp'] : 0); // Add the supplement
  }
  // Get a total price for adults + children + supplements for all rooms
  $array['grandTotal'] = array_sum($array['roomTotalPrice']);
  return $array;
}

$iroom = array (
  'num_rooms' => 2,
  'adults' => array (
    0 => 2,
    1 => 3
  ),
  'childern' => array (
    0 => 1,
    1 => 2
  ),
  'prices' => array (
    0 => 44.5,
    1 => 44.5
  ),
  'price' => 178,
);

print_r(calculateTotalPrices($iroom));
/* With the above array, outputs
Array
(
    [num_rooms] => 2
    [adults] => Array
        (
            [0] => 2
            [1] => 3
        )

    [childern] => Array
        (
            [0] => 1
            [1] => 2
        )

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

    [price] => 178
    [roomTotalPrice] => Array
        (
            [0] => 119
            [1] => 183.5
        )

    [grandTotal] => 302.5
)
*/

print_r(calculateTotalPrices($iroom,20,25));
/* With your sample array, outputs
Array
(
    [num_rooms] => 2
    [adults] => Array
        (
            [0] => 2
            [1] => 3
        )

    [childern] => Array
        (
            [0] => 1
            [1] => 2
        )

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

    [price] => 178
    [roomTotalPrice] => Array
        (
            [0] => 154
            [1] => 243.5
        )

    [grandTotal] => 397.5
)
*/      

使用其他检查更新的代码

foreach ($iroom as $k1 => $v1) { // Loop outer array
  if (is_array($v1)) { // Make sure element is an array
    foreach ($v1 as $k2 => $v2) { // Loop inner array
      if (is_array($v2)) { // Make sure element is an array
        for ($i = 0; $i < $v2['num_rooms']; $i++) { // Loop num_rooms times
          $iroom[$k1][$k2]['roomTotalPrice'][$i] =
            ((isset($v2['adults'][$i],$v2['prices'][$i])) ? ($v2['adults'][$i] * $v2['prices'][$i]) + ($v2['adults'][$i] * $asup) : 0) // Calculate price for adults
            + ((isset($v2['childern'][$i])) ? ($v2['childern'][$i] * $csup) : 0) // Calculate price for children
            + $v2['supp']; // Add the supplement
        }
        // Get a total price for adults + children + supplements for all rooms
        $iroom[$k1][$k2]['grandTotal'] = array_sum($iroom[$k1][$k2]['roomTotalPrice']);
      }
    }
  }
}

print_r($iroom);

修改

使用上面的确切代码,输入上面的数组,并在顶部设置$asup = $csup = 10;,我没有错误,这个输出:

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

                    [childern] => Array
                        (
                            [0] => 1
                            [1] => 2
                        )

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

                    [price] => 178
                    [supp] => 0
                    [roomTotalPrice] => Array
                        (
                            [0] => 119
                            [1] => 183.5
                        )

                    [grandTotal] => 302.5
                )

        )

)

请注意,第一个结果出现在119,而不是129,正如您在上面的注释中所述 - 这是因为在您的示例数组中,supp为0而不是10,正如您在计算中所使用的那样。

我还测试过更复杂的数组(在第一级和第二级有更多元素),它运行正常。

我猜你是否得到“为foreach提供的无效参数”错误,因为在你的实际数组中,你的最高级别有一些非数组成员。通过更改

可以轻松克服这个问题
foreach ($v1 as $k2 => $v2) {

if (is_array($v1)) foreach ($v1 as $k2 => $v2) {