1 <?
php
2 $arrs =
Array (
3 Array ( 'rname' => '小麦', 'm1'=> 12.00 ,'m2'=> 33,'qq'=> 33 ) ,
4 Array ( 'rname' => '小麦', 'm1' => 23.00,'m2'=> 44,'qq'=> 11 ) ,
5 Array ( 'rname' => 'ccc', 'm1' => 11,'m2'=> 22,'qq'=> 12
)
6 );
7
8 //计算出重复字段出现的次数
9 $b=
array();
10 foreach(
$arrs as $v){
11 $b[]=
$v['rname'
];
12 }
13 $c=
array_unique(
$b);
14 foreach(
$c as $v){
15 $n=0
;
16 foreach(
$arrs as $t){
17 if(
$v==
$t['rname'
])
18 $n++
;
19 }
20 $d[]['nn']=
$n;
21
22 }
23
24 //合并数组中重复的值,并进行加计算
25 $tmpval=
array();
26 $data=
array();
27 $i=0
;
28 //遍历原数据
29 foreach(
$arrs as $key=>
$arr){
30
31 if(!
empty(
$tmpval) &&
array_key_exists(
$arr['rname'],
$tmpval)){
32 //存在
33 $key=
$tmpval[
$arr['rname'
]];
34
35 $data[
$key]['m1']+=
$arr['m1'
];
36
37 $data[
$key]['m2']+=
$arr['m2'
];
38 }
else{
39 //不存在
40 $tmpval[
$arr['rname']]=
$i;
41 $data[
$i++]=
$arr;
42 }
43 }
44
45 //把2个二维数组合并成一个二维数组
46 $zuizhong =
array();
47 foreach(
$data as $k=>
$r){
48 $zuizhong[] =
array_merge(
$r,
$d[
$k]);
49 }
50
51 //计算出平均值
52 $datas =
array();
53 foreach(
$zuizhong as $key=>
$arr){
54 $datas[
$key]['rname'] =
$arr['rname'
];
55 $datas[
$key]['m1'] =
$arr['m1']/
$arr['nn'
];
56 $datas[
$key]['m2'] =
$arr['m2']/
$arr['nn'
];
57 }
58
59 echo "<pre>"
;
60 print_r(
$d);
61 print_r(
$data);
62 print_r(
$zuizhong);
63 print_r(
$datas);
http://www.cnblogs.com/losesea/archive/2013/06/14/3134900.html
转载于:https://www.cnblogs.com/liruning/p/6112882.html