The basic count function

The count function in it's simplest form is used to count the number of elements in an array.

The $fruit array contains 2 elements: 'apples' and 'oranges'. The count function in this case returns the value 2. It also functions as you would expect with an associative array:

The $scores array contains 3 key-value pairs, the count function for the above example returns 3. Note that NULL values are also counted i.e the count function does not validate the actual values, it only returns the number of values.

Counting values in an array

PHP also has a related count function for arrays array_count_values. The array_count_values function counts the number of occurrances of each string or integer in an array.

The array_count_values function returns an associative array, the keys are the values of the input array and the values are their count, e.g: a is 2 and b is 1.

Note that the array_count_values function can only count string and integer values. The above example results in array_count_values(): Can only count STRING and INTEGER values!

Count of unique values

The array_unique function returns the unique values of an array, i.e duplicate array values are removed. The array_unique example below removes the duplicate 'a' value.

The array_unique function also works on associative arrays.

This is not the same as the count of unique array values. The unique values in the $letters array above, are: b and c. While the unique values in the $food array, are: banana and carrot.

For us to find the count of unique values in an array, we can use a combination of count, array_filter and array_count_values. Let's use the $letters array as our example, we first use array_count_values to return the count of each value in the array:

We can see from the output above that the unique letters are b and c. Now we can use array_filter to keep the values which are equal to 1.

And finally count those values:

Counting duplicate array values

We can easily find the count of duplicate array values using a combination of count and array_unique. We can count the original number of array values and subtract the number of unique array values, here are a few examples:

Count a specific value

To count a specific array value, we can use count and array_filter. We'll use array_filter with a function to keep the value we want to count and then use count to return the number of values.

Counting a specific value using array_filter is roughly the equivalent of count if, our function is the if which determines what values are returned and then counted.

Array count vs sizeof

The sizeof function is an alias of the count function. The sizeof function in PHP does not return the amount of memory allocated by a variable, as with other languages.

Key Takeaways

  • We can use the count function to return the number of values in an array.
  • The array_count_values function counts the number of occurances of each value in an array.
  • array_unique removes duplicate values in an array.
  • array_filter works with a callback function to filter out values from an array.
  • The sizeof function is simply an alias of count.