首頁  >  文章  >  後端開發  >  关于处置数组中同类统计的解决方案(PHP)

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

WBOY
WBOY原創
2016-06-13 13:10:38714瀏覽

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

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn