关于处置数组中同类统计的解决方案(PHP)

WBOY
发布: 2016-06-13 13:10:38
原创
714 人浏览过

关于处理数组中同类统计的解决方案(PHP)

$arrItem = array(
Array (10000,'中国',1),
Array (10000,'中国',3),
Array (20000,'美国',2),
Array (30000,'法国',5)
);

$arr = array();
foreach($arrItem as $v){
??? $arr[$v[0]] = isset($arr[$v[0]]) ? $arr[$v[0]] + $v[2] : $v[2];
??? print_r($arr);
??? echo '
';
}

?

?

?

$arr = array(
??? array('countyid' => 1, 'county' => '中国', 'province' => '湖南'),
??? array('countyid' => 1, 'county' => '中国', 'province' => '湖南'),
??? array('countyid' => 3, 'county' => '日本', 'province' => '名古屋'),
??? array('countyid' => 3, 'county' => '日本', 'province' => '东京'),
??? array('countyid' => 1, 'county' => '中国', 'province' => '北京'),
??? array('countyid' => 1, 'county' => '中国', 'province' => '山东'),
??? array('countyid' => 1, 'county' => '中国', 'province' => '山东'),
??? array('countyid' => 2, 'county' => '美国', 'province' => '安大略'),
??? array('countyid' => 2, 'county' => '美国', 'province' => '安大略'),
??? array('countyid' => 3, 'county' => '日本', 'province' => '名古屋'),
);

$countyGroupCount = array();
$countyProvinceGroupCount = array();

foreach ($arr as $row) {
??? $countyKey = $row['county'];
??? $countyProvinceKey = $row['county'] . '-' . $row['province'];

??? if (!isset($countyGroupCount[$countyKey])) {
??????? $countyGroupCount[$countyKey] = 1;
??? } else {
??????? $countyGroupCount[$countyKey] += 1;
??? }

??? if (!isset($countyProvinceGroupCount[$countyProvinceKey])) {
??????? $countyProvinceGroupCount[$countyProvinceKey] = 1;
??? } else {
??????? $countyProvinceGroupCount[$countyProvinceKey] += 1;
??? }
}

print_r($countyGroupCount);
print_r($countyProvinceGroupCount);

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板