2

我不确定在这方面要搜索什么功能,所以似乎无法帮助自己。看起来确实是一个明显的问题。我有一个数组,在我有重复键的地方,我希望添加这些值。例如:

这是我阵列的一部分。

[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

4 回答 4

13
<?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
        )

)
于 2013-02-19T13:01:11.380 回答
1

假设,$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。你将不得不操纵它。

于 2013-02-19T13:09:51.750 回答
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,所以它现在可以工作了。

于 2013-02-19T12:59:24.147 回答
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
        )

)
于 2013-02-19T13:20:22.877 回答