0

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

1
  • Perhaps post the code you have now so that we can try to optimize it? Commented Aug 14, 2010 at 5:31

4 Answers 4

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Column counting in incorrect here. You are adding rows total result into column total result.
What's the column total supposed to be?
Means total of each column elements. Look at my example in my question. $twoDimArr['columnTotal'][0] should be the sum of all first elements from each row. $twoDimArr['columnTotal'][1] should be the sum of all 2nd elements from each row.
2

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

Comments

1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.