当前位置: 动力学知识库 > 问答 > 编程问答 >

Is there a PHP function to count the number of times a value occurs within an array?

问题描述:

I need to count the number of times a value occurs in a given array.

For example:

$array = array(5, 5, 2, 1);

// 5 = 2 times

// 2 = 1 time

// 1 = 1 time

Does such a function exist? If so, please point me to it in the php docs... because I can't seem to find it.

Thanks.

网友答案:

Yes, it's called array_count_values().

$array = array(5, 5, 2, 1);
$counts = array_count_values($array); // Array(5 => 2, 2 => 1, 1 => 1)
网友答案:

array_count_values

分享给朋友:
您可能感兴趣的文章:
随机阅读: