PHP组数组值然后求和

时间:2018-06-11 15:47:15

标签: php arrays algorithm multidimensional-array

我如何按成人价格分组然后获得成人数的总和。这是我的阵列:

    Array
        (
        [0] => Array
            (
                [Reservation] => Array
                    (
                        [adult_price] => 2000
                        [adult_count] => 2
                    )
            )

        [1] => Array
            (
                [Reservation] => Array
                    (
                        [adult_price] => 2000
                        [adult_count] => 1
                    )
            )

        [2] => Array
            (
                [Reservation] => Array
                    (
                        [adult_price] => 300
                        [adult_count] => 1
                    )
            )
    )

如果adult_price与adult_count将添加的时间相同,则预期结果:

Array
    (
    [0] => Array
        (
            [Reservation] => Array
                (
                    [adult_price] => 2000
                    [adult_count] => 4
                )
        )

    [1] => Array
        (
            [Reservation] => Array
                (
                    [adult_price] => 300
                    [adult_count] => 1
                )
        )
)

请帮帮我谢谢!

3 个答案:

答案 0 :(得分:0)

尝试这样的事情:

function summarize($ReservationArr){
  $summed = [];
  foreach ($ReservationArr as $one){

    $adult_price = $one["Reservation"]["adult_price"];
    $adult_count= $one["Reservation"]["adult_count"];
    if (isset( $summed[$adult_price])){
            $summed[$adult_price] += (int)$adult_count;
    } else {
          $summed[$adult_price] = (int)$adult_count;
        }
  }
    // now rebuild:
    $result = [];
    foreach ($summed as $price => $number){
        $result[] = array("Reservation" => array("adult_price" => $price, "adult_count" => $number));
    }
    return $result;
}

$test = [];
$test[] = array("Reservation" => array("adult_price" => 2000, "adult_count" => 2));
$test[] = array("Reservation" => array("adult_price" => 1000, "adult_count" => 1));
$test[] = array("Reservation" => array("adult_price" => 500, "adult_count" => 4));
$test[] = array("Reservation" => array("adult_price" => 2000, "adult_count" => 3));

$test[] = array("Reservation" => array("adult_price" => 2000, "adult_count" => 2));
$test[] = array("Reservation" => array("adult_price" => 1000, "adult_count" => 1));
$test[] = array("Reservation" => array("adult_price" => 500, "adult_count" => 4));
$test[] = array("Reservation" => array("adult_price" => 2000, "adult_count" => 3));

$res = summarize($test);
echo "<pre>";
print_r($res);
echo "</pre>";

答案 1 :(得分:0)

试试这个:

$grouped = [];

foreach ($array as $item) {
    $item = $item["Reservation"];
    $currentValue = isset($grouped[$item["adult_price"]]) ? $grouped[$item["adult_price"]] : 0;
    $grouped[$item["adult_price"]] = $currentValue + $item["adult_count"];
}

$buildArray = [];

foreach ($grouped as $price => $count) {
    $buildArray[] = ["Reservation" => ["adult_price" => $price, "adult_count" => $count]];
}

print_r($buildArray);

https://3v4l.org/iZZ4m

答案 2 :(得分:0)

使用两个循环是不必要的工作。将输出数组视为不断更新的查找数组。这将确定您要处理的是新的Adult_price还是之前遇到的价格。使用$row['Reservation']['adult_price']值作为新的临时第一级密钥-这有助于isset()知道是否应该保存整行,或者是否应该将adult_count添加到存储的计数中。

代码:(Demo

$rows = [
    ["Reservation" => ["adult_price" => 2000, "adult_count" => 2]],
    ["Reservation" => ["adult_price" => 1000, "adult_count" => 1]],
    ["Reservation" => ["adult_price" => 500, "adult_count" => 4]],
    ["Reservation" => ["adult_price" => 2000, "adult_count" => 3]],
    ["Reservation" => ["adult_price" => 2000, "adult_count" => 2]],
    ["Reservation" => ["adult_price" => 1000, "adult_count" => 1]],
    ["Reservation" => ["adult_price" => 500, "adult_count" => 4]],
    ["Reservation" => ["adult_price" => 2000, "adult_count" => 3]],
];

$result = [];
foreach ($rows as $row) {
    if (!isset($result[$row['Reservation']['adult_price']])) {
        $result[$row['Reservation']['adult_price']] = $row;
    } else {
        $result[$row['Reservation']['adult_price']]['Reservation']['adult_count'] += $row['Reservation']['adult_count'];
    }
}
var_export(array_values($result));

或者,这可能会减少眼睛疲劳:

foreach ($rows as $row) {
    $key = $row['Reservation']['adult_price'];
    if (!isset($result[$key])) {
        $result[$key] = $row;
    } else {
        $result[$key]['Reservation']['adult_count'] += $row['Reservation']['adult_count'];
    }
}

输出:(来自任一片段)

array (
  0 => 
  array (
    'Reservation' => 
    array (
      'adult_price' => 2000,
      'adult_count' => 10,
    ),
  ),
  1 => 
  array (
    'Reservation' => 
    array (
      'adult_price' => 1000,
      'adult_count' => 2,
    ),
  ),
  2 => 
  array (
    'Reservation' => 
    array (
      'adult_price' => 500,
      'adult_count' => 8,
    ),
  ),
)