php数组的统计解决办法

WBOY
Release: 2016-06-13 13:33:56
Original
908 people have browsed it

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


------解决方案--------------------

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); <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template