表中列的总计

时间:2017-01-26 12:52:21

标签: php laravel laravel-4

我想计算表格中一系列列的总数,我该如何做呢?

以下是我要总结的数据示例:

$ python2
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> data = ["Header: 1", \
...         "Some info 1", \
...         "Some info 2", \
...         "Some info 3",\
...         "Header: 2",\
...         "Some info 4",\
...         "Header: 3", \
...         "Some info 5",\
...         "Some info 6",\
...         ]
>>> 
>>> entries = []
>>> entry = None
>>> for element in data:
...     if "Header" in element:
...         entry = []
...         entries.append(entry)
...     entry.append(element)
... 
>>> entries
[['Header: 1', 'Some info 1', 'Some info 2', 'Some info 3'], ['Header: 2', 'Some info 4'], ['Header: 3', 'Some info 5', 'Some info 6']]

我想将每个索引加在一起,例如:对于Jim Mayor__targeted_target,索引0添加到Bob Martinez__targeted_target的索引0(这给出了1月的总数)。它需要与无限数量的交易者合作。

生成数据的函数:

array:1 [
  "traders" => array:6 [
    "Jim Mayor__targeted_target" => array:2 [
      "amounts" => array:13 [
        0 => 5
        1 => 5
        2 => 0
        3 => 0
        4 => 0
        5 => 0
        6 => 0
        7 => 0
        8 => 0
        9 => 0
        10 => 0
        11 => 0
      ]
      "row_name" => "Targeted target"
    ]
    "Jim Mayor__actual_targeted" => array:2 [
      "amounts" => array:13 [
        0 => 0
        1 => 1
        2 => 0
        3 => 0
        4 => 0
        5 => 0
        6 => 1
        7 => 1
        8 => 0
        9 => 0
        10 => 0
        11 => 0
      ]
      "row_name" => "Actual targeted"
    ]
    "Bob Martinez__targeted_target" => array:2 [
      "amounts" => array:13 [
        0 => 1
        1 => 0
        2 => 0
        3 => 0
        4 => 0
        5 => 0
        6 => 0
        7 => 0
        8 => 0
        9 => 0
        10 => 0
        11 => 0
      ]
      "row_name" => "Targeted target"
    ]
    "Bob Martinez__actual_targeted" => array:2 [
      "amounts" => array:13 [
        0 => 19
        1 => 45
        2 => 20
        3 => 26
        4 => 21
        5 => 10
        6 => 12
        7 => 20
        8 => 11
        9 => 2
        10 => 0
        11 => 0
      ]
      "row_name" => "Actual targeted"
    ]
...

4 个答案:

答案 0 :(得分:0)

只需检查数据并将结果保存在其他数组中:

$results = array();

foreach ($traders as $trader) {
    foreach ($trader['amounts'] as $i => $amount) {
        if (!isset($results[$i])) {
            $results[$i] = 0;
        }

        $results[$i] += $amount;
    }
}

结果数组将包含所有交易者的总和。

未经测试但应该有效。

答案 1 :(得分:0)

做这样的事情:

function sumRowsByMonth($traders)
{
    $sums = array_fill(0, 12, 0);
    foreach($traders as $trader) {
        foreach($trader['amounts'] as $monthId => $amount) {
            $sums[$monthId] += $amount;
        }
    }
    return $sums;
}

我测试了这个:

$arr = array(
    'traders' => array(
        'Jim Mayor__targeted_target' => array(
            'amounts' => array(
                0  => 5,
                1  => 5,
                2  => 0,
                3  => 0,
                4  => 0,
                5  => 0,
                6  => 0,
                7  => 0,
                8  => 0,
                9  => 0,
                10 => 0,
                11 => 0,
            )
        ),
        'Bob Martinez__targeted_target' => array(
            'amounts' => array(
                0  => 19,
                1  => 45,
                2  => 20,
                3  => 26,
                4  => 21,
                5  => 10,
                6  => 12,
                7  => 20,
                8  => 11,
                9  => 2,
                10 => 0,
                11 => 0,
            )
        ),
    ),
);
var_dump(sumRowsByMonth($arr['traders']));

答案 2 :(得分:0)

使用array_sum()功能。

  

array_sum()返回数组中值的总和

另外,您说您想分别计算targeted targetactual targeted

$actual = [];
$targeted = [];

foreach ($traders as $trader) {
    $sum = array_sum($trader['anmounts']);
    $trader['row_name'] === 'Actual targeted') ? $actual += $sum : $target += $sum;
}

也许你需要稍微修改一下来调整行为,但我想我有了这个想法。

答案 3 :(得分:0)

@ imperium2335:

  

我不断得到的主要问题是汇总所有行,包括actual_targeted。 actual_targeted需要分组并分别汇总到targeted_target。

然后这样做:

function sumRowsByMonth($traders)
{
    $sums = array();

    foreach($traders as $traderName => $trader) {
        $type = strstr($traderName, '__');
        if( empty($sums[$type]) ) {
            $sums[$type] = array_fill(0, 12, 0);
        }

        foreach($trader['amounts'] as $monthId => $amount) {
            $sums[$type][$monthId] += $amount;
        }
    }
    return $sums;
}

阅读本文: http://php.net/manual/en/function.strstr.php