我有一个像这样的数组,我希望它的总和如下所述:
Array
(
[0] => stdClass Object
(
[id] => 1
[group] => 1
)
[1] => stdClass Object
(
[id] => 2
[group] => 2
)
[2] => stdClass Object
(
[id] => 3
[group] => 2
)
[3] => stdClass Object
(
[id] => 4
[group] => 3
)
[4] => stdClass Object
(
[id] => 5
[group] => 3
)
[5] => stdClass Object
(
[id] => 6
[group] => 3
)
)
我希望结果显示如下:
第1组有1名成员。第2组有2名成员。第3组有3名成员。
答案 0 :(得分:0)
执行以下代码可以帮助您。
$arr_ids = array(array('id'=>10,'group'=> '1'),
array('id'=>20,'group'=> '1'),
array('id'=>30,'group'=> '2'),
array('id'=>40,'group'=> '3'),
array('id'=>50,'group'=> '3'),
array('id'=>60,'group'=> '3'),
array('id'=>70,'group'=> '4'),
);
foreach($arr_ids as $ids)
{
$arr_groups[] = $ids['group'];
}
$count_arr = array_count_values($arr_groups);
print"<pre>";
print_r($arr_groups);
print_r($count_arr);
foreach($count_arr as $group=>$count)
{
echo "Group ".$group." = ".$count."<br>";
}
print"</pre>";