Rumah > Artikel > pembangunan bahagian belakang > php数组的统计解决办法
php数组的统计
我有这样的一个数组,不知道怎么来统计,A\B\c的分数
Array
(
[0] => Array
(
[0] => A
[1] => A因素
[2] => 1 //A因数的分数
)
[1] => Array
(
[0] => A
[1] => A因素
[2] => 3
)
[2] => Array
(
[0] => A
[1] => A因素
[2] => 6
)
[3] => Array
(
[0] => A
[1] => A因素
[2] => 6
)
[4] => Array
(
[0] => A
[1] => A因素
[2] => 1
)
[5] => Array
(
[0] => B
[1] => B因素
[2] => 1
)
[6] => Array
(
[0] => B
[1] => B因素
[2] => 2
)
[7] => Array
(
[0] => B
[1] => B因素
[2] => 3
)
[8] => Array
(
[0] => B
[1] => B因素
[2] => 1
)
[9] => Array
(
[0] => C
[1] => C因素
[2] => 1
)
[10] => Array
(
[0] => C
[1] => C因素
[2] => 5
)
)
------解决方案--------------------
$ar = array( array('A','A因素',1), array('A','A因素',3), array('A','A因素',6), array('A','A因素',6), array('A','A因素',1), array('B','B因素',1), array('B','B因素',2), array('B','B因素',3), array('B','B因素',1), array('C','C因素',1), array('C','C因素',5) ); $result = array(); foreach($ar as $v) { if (array_key_exists($v[0], $result)) $result[$v[0]] += $v[2]; else $result[$v[0]] = $v[2]; } print_r($result); <div class="clear"> </div>