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

一、统计数组中所有的值

 

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

返回一个关联数组,

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

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

对数组里面的每个不是 string 和 integer 类型的元素抛出一个警告错误(E_WARNING)。 对于值是字符串的区分大小写
例子1:

 

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

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

 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

 

你可能感兴趣的:(GavinLau,-,php数组函数)