user3624883
user3624883

Reputation: 81

Best way to calculated how many times a value appears in an array?

I wish to calculate how many times each number appears in the array.

The numbers are all between 1 - 10

So for example: {1,2,2,5,5,6,7,8,2) Would return 3 instances for '2'. And 2 instances for '5' and 1 instance for the rest.

What is the best way to do this, other then me creating 10 for loops?

Upvotes: 1

Views: 56

Answers (1)

AbraCadaver
AbraCadaver

Reputation: 78994

array_count_values()

print_r(
    array_count_values($array)
);

Upvotes: 5

Related Questions