热搜:NVER 

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

2024-04-26 10:45:01
 关于处置数组中同类统计的解决方案(PHP)

关于处理数组中同类统计的解决方案(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);