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

Answers


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.


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

Need Your Help

JavaScript simulate Accesskey-Press

javascript jquery access-keys

I was wondering if there is a way to simulate pressing an accesskey with JavaScript. It should work with the following html:

NSNotificationCenter Standard Practice?

ios nsnotifications

Is is standard to balance the 'addObserver' with: