欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2024-02-06 17:47:52
...
关于处理数组中同类统计的解决方案(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)

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

相关文章

相关视频