Which PHP function counts all the values of an array?

array_count_values() counts how many times each value appears in an array. It returns a new array where the values become keys and the counts become values.

<?php
$fruits = ["apple", "banana", "apple", "orange", "apple"];

print_r(array_count_values($fruits));
?>

Output:

Tutorials Class - Output Window

Array
(
[apple] => 3
[banana] => 1
[orange] => 1
)

It works only with strings and integers. If you only need the total number of elements, use count().


Learn more about the similar topics:
Tutorials
PHP Arrays
PHP Array Functions
Exercises & Assignments
Remove specific element by value from an array in PHP?
PHP Array to String Conversion (favourite colours chosen by user)
How to check if an array is a subset of another in PHP?
Interview Questions & Answers
What are the different PHP functions to sort an array?
PHP’s numerically indexed array begins with which position?
Which PHP function inserts an element to the end of an array?
Which PHP function can be used check if variable is an array?
How many types of arrays are available in PHP?
What is the use of array_chunk() function in PHP?
What is the use of in_array() function in PHP?