PHP:如何获得二维数组的元素总和?

时间:2010-08-14 05:26:10

标签: php arrays

我希望得到该行末尾每行的元素总数以及每列末尾每列的元素总数。

例如:

我有一个数字值如下的数组:

$twoDimArr = array(  array("1" , "2" , "3" , "4"),
                     array("1" , "2" , "3" , "4"),
                     array("1" , "2" , "3" , "4"),
                     array("1" , "2" , "3" , "4"),
                     array("1" , "2" , "3" , "4"),
                     array("1" , "2" , "3" , "4"),
                     array("1" , "2" , "3" , "4")

);

输出如下内容:

$twoDimArr = array(  array("1" , "2" , "3" , "4", "total" => "10"),
                     array("1" , "2" , "3" , "4", "total" => "10"),
                     array("1" , "2" , "3" , "4", "total" => "10"),
                     array("1" , "2" , "3" , "4", "total" => "10"),
                     array("1" , "2" , "3" , "4", "total" => "10"),
                     array("1" , "2" , "3" , "4", "total" => "10"),
                     array("1" , "2" , "3" , "4", "total" => "10"))
                     "columnTotal => "array("7" , "14" , "21" , "28", "70")

    );

输入数组: 行中元素的数量可能不同,但每个行元素的数量不等于其他行元素。输入数组中的索引可以是任何内容。

我已对此进行编码,但它对我有用,但我对代码量不满意。可能有人会编写更高效的代码解决方案。

由于

3 个答案:

答案 0 :(得分:3)

你去吧

<?php

$twoDimArr = array(  array("1" , "2" , "3" , "4"),
                     array("1" , "2" , "3" , "4"),
                     array("1" , "2" , "3" , "4"),
                     array("1" , "2" , "3" , "4"),
                     array("1" , "2" , "3" , "4"),
                     array("1" , "2" , "3" , "4"),
                     array("1" , "2" , "3" , "4")

);  

$current = 0;
foreach($twoDimArr as $evaluate) {
    $total = 0;
    foreach($evaluate as $value) {
        $total = $total + $value;
    }
    $twoDimArr[$current]['total'] = $total;
    $twoDimArr['columnTotal'][] = $total;
    $current++;
}

print_r($twoDimArr);

?>

编辑它以包含您的列总数。

答案 1 :(得分:1)

以下是我的解决方案:

function getTotalArray( $twoDimArr ) {

    if( !empty( $twoDimArr ) && is_array( $twoDimArr ) ) {

        // Create an array to store column total
        $myArr = array();
        for( $i = 0; $i < count( end($twoDimArr) ); $i++ ) {
            $myArr[$i] = 0; 
        }


        foreach( $twoDimArr as $key => $value ) {

            $colCount = 0;
            $rowCount = 0;
            $i = 0;

            foreach( $twoDimArr[$key] as $key1 => $value1 ) {
                $colCount += $value1;
                $myArr[$i] += $value1;
                $i++;
            }

            // Add column total
            $twoDimArr[$key]['total'] = $colCount;
        }

        // Last column total that is created while row elements sum
        $myArr['totaloftotal'] = 0;
        foreach( $twoDimArr as $key2 => $values3 ) {
            $myArr['totaloftotal'] += end($twoDimArr[$key2]);
        }

        $twoDimArr = array_merge($twoDimArr, array( "total" => $myArr) );

        return $twoDimArr;

    } else {

        $twoDimArr = array();
        return $twoDimArr;
    }

} // end function

答案 2 :(得分:0)

在迭代数据行时,用 total 短暂填充新的 array_sum() 元素。

然后迭代变异行的列以保持 columnTotal 值的运行记录。

为避免生成通知和警告,请在尝试执行加法之前检查是否声明了 $twoDimArr['columnTotal'][$column]

代码:(Demo)

$twoDimArr = [
    ["1", "2", "3", "4"],
    ["1", "2", "3", "4"],
    ["1", "2", "3", "4"],
    ["1", "2", "3", "4"],
    ["1", "2", "3", "4"],
    ["1", "2", "3", "4"],
    ["1", "2", "3", "4"],
];

foreach ($twoDimArr as $i => $row) {
    $twoDimArr[$i]['total'] = array_sum($row);
    foreach ($twoDimArr[$i] as $column => $value) {
        $twoDimArr['columnTotal'][$column] = ($twoDimArr['columnTotal'][$column] ?? 0) + $value;
    }
}
var_export($twoDimArr);