-5

我有一个这样的数组,我希望它的总和如下所述:

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 名成员。

4

1 回答 1

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>";
于 2012-05-29T12:11:23.460 回答