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

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

For instance:

$array = array(5, 5, 2, 1); // 5 = 2 times // 2 = 1 time // 1 = 1 time 

Is there such a function? If yes, tell me this in php docs ... because I cannot find it.

Thanks.

+4
source share
2 answers

Yes, it is called array_count_values() .

 $array = array(5, 5, 2, 1); $counts = array_count_values($array); // Array(5 => 2, 2 => 1, 1 => 1) 
+7
source

Source: https://habr.com/ru/post/1393881/


All Articles