回复 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 编辑 ]