PHP计数数组的唯一值

时间:2013-12-08 10:21:44

标签: php arrays

我有一个类似下面的数组,我想计算一个agentName出现的次数。

基本上我想知道hello@url.comBYE@url.com发生了多少次。下面的Awnser应为hello@url.com = 3且BYE@url.com = 2

Array
(
    [0] => stdClass Object
        (
            [text_date] => 2013-12-04 19:27:29
            [name] => hello@url.com
        )

    [1] => stdClass Object
        (
            [text_date] => 2013-12-07 19:18:32
            [name] => hello@url.com
        )

    [2] => stdClass Object
        (
            [text_date] => 2013-12-08 09:59:30
            [name] =>  hello@url.com
        )

    [3] => stdClass Object
        (
            [text_date] => 2013-12-04 12:23:24
            [name] => BYE@url.com
        )

    [4] => stdClass Object
        (
            [text_date] => 2013-12-04 13:10:18
            [name] => BYE@url.com
        )
)

如果有可能,有什么想法吗?

1 个答案:

答案 0 :(得分:1)

如果你使用的是PHP 5.5,你可以这样做

$new_array = array_count_values(array_values($yourarray,'name'));
foreach($new_array as $k=>$v)
{
echo "$k occurred $v times\n";
}