代码之家  ›  专栏  ›  技术社区  ›  Naveed

php:如何得到二维数组的元素之和?

  •  0
  • Naveed  · 技术社区  · 14 年前

    我想得到该行末尾每行的元素总数和每列末尾每列的元素总数。

    例如:

    我有一个数组,其数字值如下:

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

    输入数组: 行中的元素数量可能会有所不同,但每个行元素的数量都不会与其他行元素相同。输入数组中的索引可以是任何内容。

    我已经对它进行了编码,它对我很有用,但是我对代码的数量不满意。可能有人会编码更高效,代码更少的解决方案。

    谢谢

    2 回复  |  直到 14 年前
        1
  •  3
  •   Raphael Caixeta    14 年前

    干得好

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

    编辑它以包括列汇总。

        2
  •  1
  •   Naveed    14 年前

    这是我的解决方案:

    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