array_count_values() - 统计数组中所有的值

一、统计数组中所有的值

 

array_count_values()
参数必须示例
需要统计值的数组$a = array(1, "hello", 1, "world", "hello");
返回值错误 / 异常注意事项

返回一个关联数组,

用参数数组中的值作为键名,

用该键名在参数数组中出现的次数作为值

对数组里面的每个不是 string 和 integer 类型的元素抛出一个警告错误(E_WARNING)。对于值是字符串的区分大小写
例子1:
<?php
    $a = array(1, "hello", 1, "world", "hello");
    print_r(array_count_values($a));

 

返回结果:
Array
(
    [1] => 2
    [hello] => 2
    [world] => 1
)

例子2 : 在多维数组中找到具有特定值的项数的简单方法

<?php
    $list = array(
        array('id' => 1, 'userId' => 5),
        array('id' => 2, 'userId' => 5),
        array('id' => 3, 'userId' => 6),
    );
    $userids = array_column($list, 'userId');
    $count = array_count_values($userids);
    $find_userid = 5;
    echo $count[$find_userid];

 

返回结果:

2

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值