数组php中重复的总和

时间:2013-02-19 12:48:26

标签: php arrays

我不确定要搜索的功能是什么,所以似乎无法帮助自己。看起来真的是一个明显的问题。 我有一个数组,并且我有重复的键,我希望添加值。 e.g:

这是我阵列的一部分。

[1] => Array
    (
        [inputAvg] => 21.41 KB
        [outputAvg] => 22.03 KB
        [date] => 2011-08-01
    )

[2] => Array
    (
        [inputAvg] => 182.63 KB
        [outputAvg] => 186.05 KB
        [date] => 2011-08-01
    )

[3] => Array
    (
        [inputAvg] => 182.63 KB
        [outputAvg] => 186.05 KB
        [date] => 2011-08-02
    )

[4] => Array
    (
        [inputAvg] => 4.84 MB
        [outputAvg] => 4.93 MB
        [date] => 2011-08-03
    )

我想做的就是说,数组的日期键是相同的(例如2011-08-01)我想要显示这个日期一次,但是重复项的组合值.... ?

例如

[1] => Array
    (
        [inputAvg] => 204.04 KB
        [outputAvg] => 208.08 KB
        [date] => 2011-08-01
    )


[2] => Array
    (
        [inputAvg] => 182.63 KB
        [outputAvg] => 186.05 KB
        [date] => 2011-08-02
    )

[3] => Array
    (
        [inputAvg] => 4.84 MB
        [outputAvg] => 4.93 MB
        [date] => 2011-08-03
    )

4 个答案:

答案 0 :(得分:10)

<?php

$array  = array(Array("inputAvg" => 21.41,"outputAvg" => 22.03,"date" => "2011-08-01"),
                Array("inputAvg" => 182.63,"outputAvg" => 186.05,"date" => "2011-08-01" ),
                Array("inputAvg" => 182.63, "outputAvg" => 186.05,"date" => "2011-08-02")
                );

$res  = array();
foreach($array as $vals){
    if(array_key_exists($vals['date'],$res)){
        $res[$vals['date']]['inputAvg']    += $vals['inputAvg'];
        $res[$vals['date']]['outputAvg']   += $vals['outputAvg'];
        $res[$vals['date']]['date']        = $vals['date'];
    }
    else{
        $res[$vals['date']]  = $vals;
    }
}

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

?>

输出:

Array
(
    [2011-08-01] => Array
        (
            [inputAvg] => 204.04
            [outputAvg] => 208.08
            [date] => 2011-08-01
        )

    [2011-08-02] => Array
        (
            [inputAvg] => 182.63
            [outputAvg] => 186.05
            [date] => 2011-08-02
        )

)

答案 1 :(得分:0)

试试这个;

$new = array() ;

foreach ($stats as $traffic){
  $key = $traffic['date'] ;

  if (isset($new[$key])){
    if ($new[$key]['date'] === $traffic['date']){
      $new[$key]['inputAvg'] += $traffic['inputAvg'] ;
      $new[$key]['outputAvg'] += $traffic['outputAvg'] ;
    }
  } else {
    $new[$key] = $traffic ;
  }
}

var_dump($new) ;

编辑TYPO,现在可以正常使用。

答案 2 :(得分:0)

假设$ data包含您要处理的所有数据

$dateArray = array();
foreach($data as $key => $value){
    if(in_array($value['date'], $dateArray)){
        $newArray[$value['date']]['inputAvg'] = $value['inputAvg'] + $newArray[$value['date']]['inputAvg'];
        $newArray[$value['date']]['outputAvg'] = $value['outputAvg'] + $newArray[$value['date']]['outputAvg'];
    }
    else{
        $dateArray[] = $value['date'];
        $newArray[$value['date']] = $value;
    }
}

但请记住,这个添加只会添加你的平均值而不会显示KB / MB。你将不得不操纵它。

答案 3 :(得分:0)

这是一个考虑了KB,MB和GB

的解决方案

从KB,MB和GB

获取绝对值的函数
function mul($unit) {
  $mul = 1;
  switch($unit) {
    case 'GB': $mul *= 1000;
    case 'MB': $mul *= 1000;
    case 'KB': $mul *= 1000;
  }
  return $mul;
}

从数字中创建字符串的功能,除以G,M或K并添加后缀

function demul($val) {
  $units = array('GB','MB','KB');
  $unit = '  ';
  $m = 1000000000;
  for ($i=0 ; $i<3 ; $i++) {
    if ($val >= $m) {
      $val /= $m;
      $unit = $units[$i];
      break;
    }
    $m /= 1000;
  }
  return number_format($val, 2) . ' ' . $unit;
}

主循环,$arr是原始数组;使用求和数据填充dates数组

$dates = array();
foreach ($arr as $key => $a) {
  $d = $a['date'];
  $i = explode(' ', $a['inputAvg']);
  $o = explode(' ', $a['outputAvg']);
  $in  = $i[0] * mul($i[1]);
  $out = $o[0] * mul($o[1]);

  if ( ! isset($dates[$d])) {
    $dates[$d] = array($in, $out);
  }
  else {
    $dates[$d][0] += $in;
    $dates[$d][1] += $out;
  }
}

根据原始格式制作result数组

$result = array();
$n = 1;
foreach ($dates as $d => $a) {
  $result[$n++] = array('date' => $d, 'inputAvg' => demul($a[0]), 'outputAvg' => demul($a[1]));
}

打印结果

print_r($result);

鉴于数据

$arr = array(
'1' => Array
    (
        'inputAvg' => '21.41 KB',
        'outputAvg' => '22.03 KB',
        'date' => '2011-08-01',
    ),

'2' => Array
    (
        'inputAvg' => '182.63 KB',
        'outputAvg' => '186.05 KB',
        'date' => '2011-08-01',
    ),

'3' => Array
    (
        'inputAvg' => '182.63 KB',
        'outputAvg' => '186.05 KB',
        'date' => '2011-08-02',
    ),

'4' => Array
    (
        'inputAvg' => '4.84 MB',
        'outputAvg' => '4.93 MB',
        'date' => '2011-08-03',
    )
);

它给出输出

Array
(
    [1] => Array
        (
            [date] => 2011-08-01
            [inputAvg] => 204.04 KB
            [outputAvg] => 208.08 KB
        )

    [2] => Array
        (
            [date] => 2011-08-02
            [inputAvg] => 182.63 KB
            [outputAvg] => 186.05 KB
        )

    [3] => Array
        (
            [date] => 2011-08-03
            [inputAvg] => 4.84 MB
            [outputAvg] => 4.93 MB
        )

)