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
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.