开发者

PHP: How to get sum of elements of 2 dimension array?

I want to get total of elements of each row at the end of that row and total of elements of each column at the end of each column.

For Example:

I have an array with digits values like this:

$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")

);

output something like this:

$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")

    );

Input Array: No of elements in rows may vary but no of each row element will be equal to other rows elements. indexes in input array may be anything.

I have coded this and it is working for me but I am not happy with the amount of code. May be someone will code more efficient and less code solution.

Thanks


Here you go

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

?>

Edited it to include your column total.


I hope it helps you out.

$total = [];
foreach ($twoDimArr as $arrayData) {
    foreach ($arrayData as $key => $value) {
        if (empty($total[$key])) {
            $total[$key] = 0;
        }
        $total[$key] = $total[$key] + $value;
    } 
}
var_dump($total);


Here is my solution:

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


As you iterate your rows of data, populate the new total element briefly with array_sum().

Then iterate the columns of the mutated row to keep a running tally of the columnTotal values.

To avoid generating notices and warnings, check if $twoDimArr['columnTotal'][$column] is declared before trying to perform addition.

Code: (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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜