tags:

views:

75

answers:

2

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

+2  A: 

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.

Raphael Caixeta
Column counting in incorrect here. You are adding rows total result into column total result.
NAVEED
What's the column total supposed to be?
Raphael Caixeta
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.
NAVEED
A: 

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
NAVEED