如何在php中总结相同的数组

时间:2014-01-30 11:16:31

标签: php arrays sum

我想用相同的键和不同的值对数组求和。

Array
(
    0 => Array
        (
            'locker_id' => 3,
            'locker_model' => 1,
            'qty' => 2
        ),

1 => Array
    (
        'locker_id' => 3,
        'locker_model' => 1,
        'qty' => 1
    ),

2 => Array
    (
        'locker_id' => 2,
        'locker_model' => 1,
        'qty' => 2
    ),

 3 => Array
    (
        'locker_id' => 3,
        'locker_model' => 1,
        'qty' => 2
    ),
4 => Array
    (
        'locker_id' => 2,
        'locker_model' => 1,
        'qty' => 2
    )

);

我想要输出

Array
(
    0 => Array
        (
            'locker_id' => 3,
            'locker_model' => 1,
            'qty' => 5
        ),
1 => Array
    (
        'locker_id' => 2,
        'locker_model' => 1,
        'qty' => 4
    )
);

感谢。

6 个答案:

答案 0 :(得分:2)

您可以使用“locker_id-locker_model”等密钥创建新数组时浏览数组,然后您可以使用array_key_exists函数轻松检查此数组中是否存在锁定器。

$inputArray = array( ... ); // this is your message array
$outputArray = array();

foreach( $inputArray as $locker )
{
    if( !array_key_exists( $locker["locker_id"]."-".$locker["locker_model"], $outputArray ) )
    {
        $outputArray[ $locker["locker_id"]."-".$locker["locker_model"] ] = array(
            "locker_id" => $locker["locker_id"],
            "locker_model" => $locker["locker_model"],
            "qty" => 0
        );
    }

    $outputArray[ $locker["locker_id"]."-".$locker["locker_model"] ]["qty"]++;
}

var_dump( $outputArray );

答案 1 :(得分:1)

    $dataArr = array
    (
        0 => array
            (
                'locker_id' => 3,
                'locker_model' => 1,
                'qty' => 2
            ),

        1 => array
            (
                'locker_id' => 3,
                'locker_model' => 1,
                'qty' => 1
            ),

        2 => array
            (
                'locker_id' => 2,
                'locker_model' => 1,
                'qty' => 2
            ),

         3 => array
            (
                'locker_id' => 3,
                'locker_model' => 1,
                'qty' => 2
            ),
        4 => array
            (
                'locker_id' => 2,
                'locker_model' => 1,
                'qty' => 2
            )

    );

    $sumArr = array();
    if(count($dataArr)>0){
        foreach($dataArr as $data){

            if(!isset($sumArr[$data['locker_id']])){
                $sumArr[$data['locker_id']] = $data;
            }
            $sumArr[$data['locker_id']]['qty'] +=$data['qty'];
        }
    }
  echo "<pre>";print_r($sumArr);

答案 2 :(得分:1)

$result = array();  

foreach($array as $key => $value){
  if(isset($value['locker_id'])){
     $result[$value['locker_id']]['qty'] += $value['qty'];
  }
  else{
     $result[$value['locker_id']] = $value;
  }

}

你必须遍历你的数组并将它保存到另一个让我们说$ result。在$ result中,您应该将locker_id作为键,然后您只需要验证该键是否存在。如果是,则添加数量值,如果不是,则必须添加整个新项目。

答案 3 :(得分:1)

考虑$ array1是你的第一个数组,

$array2 = array();
foreach($array1 as $k=>$v) {
    if(!isset($array2[$v['locker_id']])) {
        $array2[$v['locker_id']] = $v;
    } else {
        $array2[$v['locker_id']]['qty'] += $v['qty'];
    }
}

print_r($array2);

答案 4 :(得分:1)

试试这个 $ arr是你的数组

$j = 0;
$new = array();
$new[0][qty];

for($i=0;$i<arraylength;$i++){
foreach($arr as $key => $value) {
if($arr[$i][lockerid] == $arr[$key][lockerid]) {
$new[$j][lockerid] = $arr[$key][lockerid];
$new[$j][lockermodel] = $arr[$key][lockermodel];
$new[$j][qty] = $new[$i][qty] + $arr[$key][qty];
J++;
}
}
}

答案 5 :(得分:0)

试试这个

$ARR_OUTPUT = array();

    // $arr will contains your input array
    foreach($arr as $arr_val)
    {
       $locker_id = $arr_val['locker_id'];
       $locker_model = $arr_val['locker_id'];
       $qty = $arr_val['locker_id'];



       if(isset($ARR_OUTPUT[$locker_id][$locker_model]))
       {    
            $ARR_OUTPUT[$locker_id][$locker_model] += $qty;
       }
       else
       {
            $ARR_OUTPUT[$locker_id][$locker_model] = $qty;
       }



        }

    echo "<pre>";
    print_r($ARR_OUTPUT);

$ARR_OUTPUT2 = array();

    foreach($ARR_OUTPUT as $locker_id=>$arr_values)
    {
        foreach($arr_values as $locker_model=>$qty)
        {
            $arr_temp['locker_id'] = $locker_id;
            $arr_temp['locker_model'] = $locker_model;
            $arr_temp['qty'] = $qty;
            $ARR_OUTPUT2[] = $arr_temp;
        }   
    }

    echo "<pre>";
    print_r($ARR_OUTPUT2);