When working with arrays in PHP, you often need to count how many times each value appears. Instead of writing custom loops, PHP provides a built-in function called array_count_values that does this efficiently.

In this article, you’ll learn what array_count_values is, how it works, and how to use it in real-world scenarios.

The array_count_values function is a built-in PHP function used to count the frequency of values in an array. In a simple word, It returns how many times each value appears in an array.

#Arguments

  • The target array

#Returns

  • An array with keys as the values and the number of duplication as value
array_count_value

#Where is usefull?

instead of using loops to iterate on the array to detect the duplication which is not efficient, use this function easily, Also the count of the result can give you the number of values without duplication. But you can do more things using this data such as get none-duplicated values or simply the number of duplication of each value.

#Where can’t use?


Pay attention that this method only works with integers and strings beside, it’s type sensitive.