按名称分组的聚合和多维数组

时间:2016-06-22 17:25:14

标签: php arrays

我有一个数组如下

Array
(
    [0] => Array
        (
            [operation_name] => test 1
            [capacity] => 180
        )

    [1] => Array
        (
            [operation_name] => Operation 2
            [capacity] => 251
        )

    [2] => Array
        (
            [operation_name] => Operation 2
            [capacity] => 241
        )

    [3] => Array
        (
            [operation_name] => Operation 3
            [capacity] => 554
        )

)

我想总结一下"容量"键值#34; operation_name"键数组并将其作为一个数组。

在上面的数组中,我需要总结以下2个数组,

    [1] => Array
        (
            [operation_name] => Operation 2
            [capacity] => 251
        )

    [2] => Array
        (
            [operation_name] => Operation 2
            [capacity] => 241
        )

输出结果数组如下。

Array
(
    [0] => Array
        (
            [operation_name] => test 1
            [capacity] => 180
        )

    [1] => Array
        (
            [operation_name] => Operation 2
            [capacity] => 492
        )

    [2] => Array
        (
            [operation_name] => Operation 3
            [capacity] => 554
        )

)

如何用php做到这一点?

3 个答案:

答案 0 :(得分:1)

使用简单的foreach:

<?php    
$arr = Array
(
    Array
        (
            "operation_name" => "test 1",
            "capacity" => 180
        ),

    Array
        (
            "operation_name" => "Operation 2",
            "capacity" => 251
        ),

    Array
        (
            "operation_name" => "Operation 2",
            "capacity" => 241
        ),

    Array
        (
            "operation_name" => "Operation 3",
            "capacity" => 554
        )

);


$tmpArr = [];

//Finding the sum
foreach ($arr as $value) {
    $key = $value['operation_name'];
    if(array_key_exists($key, $tmpArr)){   //Check if key exists, if so add otherwise add a new entry
        $tmpArr[$key] += $value['capacity'];
    } else {
        $tmpArr[$key] = $value['capacity'];
    }
}

$finalArr = [];

//Creating desired array
foreach ($tmpArr as $key => $value) {
    $Arr['operation_name'] = $key;
    $Arr['capacity'] = $value;
    $finalArr[] = $Arr;
}

print_r($finalArr);

结果:

Array
(
    [0] => Array
        (
            [operation_name] => test 1
            [capacity] => 180
        )

    [1] => Array
        (
            [operation_name] => Operation 2
            [capacity] => 492
        )

    [2] => Array
        (
            [operation_name] => Operation 3
            [capacity] => 554
        )

)

一个活跃的人:

$finalArr = [];
foreach ($arr as $value) {
    $key = $value['operation_name'];
    if(array_key_exists($key, $finalArr)){
        $finalArr[$key]['capacity'] += $value['capacity'];
    } else {
        $finalArr[$key] = $value;
    }
}

print_r(array_values($finalArr));

答案 1 :(得分:1)

$array = array(
 array
    (
        "operation_name" => "test 1",
        "capacity" => 180
    )
,
 array
    (
        "operation_name" => "Operation 2",
        "capacity" => 252
    )
,
 array
    (
        "operation_name" => "Operation 2",
        "capacity" =>241
    )
,
array
    (
        "operation_name" => "Operation 3",
        "capacity" => 554
    )
);
$sum = 0;
$operations = array();
foreach($array as $val){
        if( !isset($operations[$val["operation_name"]] ) ){
                 $operations[$val["operation_name"]] = $val["capacity"];
        } else {
                $operations[$val["operation_name"]] += $val["capacity"];
        }
}
$final = array();
foreach( $operations as $operation => $capacity ) {
        $final[] = array('operation_name' => $operation,
                                'capacity' => $capacity);
}
print_r($final);

答案 2 :(得分:0)

我正在回答,因为现有答案出于某种原因有两个循环:

foreach($array as $val){
    if(!isset($total[$val['operation_name']] ) ){
        $total[$val['operation_name']] = $val;
    } else {
        $total[$val['operation_name']]['capacity'] += $val['capacity'];
    }
}
// reindex array
$total = array_values($total);