php数组的统计解决办法
程序员文章站
2022-04-29 13:21:26
...
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
)
)
------解决方案--------------------
我有这样的一个数组,不知道怎么来统计,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
)
)
------解决方案--------------------
- PHP code
$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);相关文章
相关视频