发新话题
打印

[php] 提供一个二维数组按列求和的函数

提供一个二维数组按列求和的函数

今天忽然用到了, 才发现原来php的二维数组没有按列求和的函数, 临时写了一个, 是可以通用的, 没做严谨性检查:
复制内容到剪贴板
代码:
function col_sum($array,$col)
{
  $sum=0;
  foreach($array as $key=>$val)
    $sum += $val[$col];
  return $sum;
}
调用很简单:
数组: $scores['name']['english']['chinese']
echo  col_sum($scores,'english'); 返回英语成绩总和.
欲知前世因,今生受者是。
欲知来世果,今生作者是。

TOP

回复 1楼 如是 的帖子

赞一下。

TOP

i think you can use array_sum to do the summation on an array
array_sum($score['english']);

TOP

引用:
原帖由 bg24254 于 2008-7-1 04:51 发表
i think you can use array_sum to do the summation on an array
array_sum($score['english']);
我也以为这样可以的, 可试了一下发现不行. $score['english']不被认为是一个array.
欲知前世因,今生受者是。
欲知来世果,今生作者是。

TOP

回复 4楼 如是 的帖子

$score['english'] is an array, here is my example, you can copy and paste to your local and run it

  1 <?php
  2
  3 $score[english]=array(90,80,70,50);
  4 $score[chinese]=array(100,95,100,85);
  5
  6 print_r($score);
  7 print 'English score total: '.array_sum($score[english])."\n";
  8
  9 if(is_array($score[english])){
10     echo "It's an array\n";
11 }else{
12     echo "It's not an array\n";
13 }
14
?>

the output of the script is following:
Array
(
    [english] => Array
        (
            [0] => 90
            [1] => 80
            [2] => 70
            [3] => 50
        )

    [chinese] => Array
        (
            [0] => 100
            [1] => 95
            [2] => 100
            [3] => 85
        )

)
English score total: 290
It's an array

TOP

回复 4楼 如是 的帖子

PHP supports multi-dimensional array, such as follow

$array = array(
                                array(
                                             array(
                                                         array(
                                                                      array()
                                                                     )
                                                          )
                                               )
                                 );

any sub-array in the $array is an array itself.

TOP

回复 5楼 bg24254 的帖子

你的好象不是二维数组吧?
是如下这样的:
复制内容到剪贴板
代码:
name    english   chinese
user1    85            30
user2    40            50
user3    77            90
用户user2的英语成绩是 $score['user2']['english']
如果用array_sum($score['english'])来计算所有人的english成绩总和报如下错误:

Warning: array_sum() [function.array-sum]: The argument should be an array in /home/www/stat.php on line 95
欲知前世因,今生受者是。
欲知来世果,今生作者是。

TOP

回复 7楼 如是 的帖子

Okay, I don't know the Chinese 二维数组 is actually the matrix, sorry about that. Then yes, you can't do the array_sum() over the english column.

To compensate my mis-understanding on the term of 二维数组, I write a recursive function for the summation on any array at any level

here is the code

  1 <?php
  2 $score  = array(
  3     'class1'=>array(
  4         'student1'=>array(
  5             'english'=>90,
  6             'chinese'=>80,
  7         ),
  8         'student2'=>array(
  9             'english'=>'75',
10             'chinese'=>'100',
11         ),
12         'student3'=>array(
13             'english'=>'100',
14             'chinese'=>'100',
15         )
16     ),
17     'class2'=>array(
18         'student1'=>array(
19             'english'=>90,
20             'chinese'=>80,
21         ),
22         'student2'=>array(
23             'english'=>'75',
24             'chinese'=>'100',
25         ),
26         'student3'=>array(
27             'english'=>'100',
28             'chinese'=>'100',
29         )
30     ),
31 );
32
33
34 print_r($score);
35 function sum($array,$targetedKey){
36     if(array_key_exists($targetedKey,$array)){
37         return $array[$targetedKey];
38     }
39
40     foreach($array as $key=>$val) {
41         $total += 0 + sum($val,$targetedKey);
42     }
43     return $total;
44 }
45 print "English score total: ".sum($score,'english')."\n";

here is the out of the script

Array
(
    [class1] => Array
        (
            [student1] => Array
                (
                    [english] => 90
                    [chinese] => 80
                )

            [student2] => Array
                (
                    [english] => 75
                    [chinese] => 100
                )

            [student3] => Array
                (
                    [english] => 100
                    [chinese] => 100
                )

        )

    [class2] => Array
        (
            [student1] => Array
                (
                    [english] => 90
                    [chinese] => 80
                )

            [student2] => Array
                (
                    [english] => 75
                    [chinese] => 100
                )

            [student3] => Array
                (
                    [english] => 100
                    [chinese] => 100
                )

        )

)
English score total: 530

[ 本帖最后由 bg24254 于 2008-7-4 00:40 编辑 ]

TOP

学习了!!
春有百花秋有月,夏有凉风冬有雪,若无闲事挂心头,便是人间好时节!

TOP

发新话题