在PHP阵列的复制总和 [英] sum of duplicates in array php

查看:93
本文介绍了在PHP阵列的复制总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林不知道的关于这个搜索功能是什么这么不能似乎帮助自己。似乎是一个明显的问题真的。
我有一个数组,并在那里我有重复键,我想补充的值。例如:

这是我的数组的一个部分。

  [1] =>排列
    (
        [inputAvg] => 21.41 KB
        [outputAvg] => 22.03 KB
        [日期] => 2011-08-01
    )[2] =>排列
    (
        [inputAvg] => 182.63 KB
        [outputAvg] => 186.05 KB
        [日期] => 2011-08-01
    )[3] =>排列
    (
        [inputAvg] => 182.63 KB
        [outputAvg] => 186.05 KB
        [日期] => 2011-08-02
    )[4] =>排列
    (
        [inputAvg] => 4.84 MB
        [outputAvg] => 4.93 MB
        [日期] => 2011-08-03
    )

我只想做的是,说其中数组之日起关键是相同的(如这里2011-08-01)我要一次显示这个日期,但有重复的项目组合值.... ?

例如

  [1] =>排列
    (
        [inputAvg] => 204.04 KB
        [outputAvg] => 208.08 KB
        [日期] => 2011-08-01
    )
[2] =>排列
    (
        [inputAvg] => 182.63 KB
        [outputAvg] => 186.05 KB
        [日期] => 2011-08-02
    )[3] =>排列
    (
        [inputAvg] => 4.84 MB
        [outputAvg] => 4.93 MB
        [日期] => 2011-08-03
    )


解决方案

 < PHP$阵列=阵列(阵列(inputAvg=> 21.41,outputAvg=> 22.03,日期=>中2011-08-01),
                阵列(inputAvg=> 182.63,outputAvg=> 186.05,日期=>中2011-08-01),
                阵列(inputAvg=> 182.63,outputAvg=> 186.05,日期=>中2011-08-02)
                );$解析度=阵列();
的foreach($数组作为$瓦尔斯){
    如果(array_key_exists($瓦尔斯['日期'],$水库)){
        $水库[$瓦尔斯['日期'] ['inputAvg'] + = $瓦尔斯['inputAvg'];
        $水库[$瓦尔斯['日期'] ['outputAvg'] + = $瓦尔斯['outputAvg'];
        $水库[$瓦尔斯['日期'] ['日期'] = $瓦尔斯['日期'];
    }
    其他{
        $水库[$瓦尔斯['日期'] = $丘壑;
    }
}回声< pre>中;
的print_r($水库);?>

输出:

 阵列

    [2011-08-01] =>排列
        (
            [inputAvg] => 204.04
            [outputAvg] => 208.08
            [日期] => 2011-08-01
        )    [2011-08-02] =>排列
        (
            [inputAvg] => 182.63
            [outputAvg] => 186.05
            [日期] => 2011-08-02
        ))

Im not sure what function to search for in regards to this so cant seem to help myself. Seems like an obvious problem really. I have an array, and where I have duplicate keys, I wish to add the values. e.g:

This is a section of my 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
    )

All I wish to do is, say where the date key of the array is the same (e.g. here 2011-08-01) I want to show this date once, but with combined values of the duplicate item....?

e.g

[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
    )

解决方案

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

?>

Output :

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
        )

)

这篇关于在PHP阵列的复制总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆