How do I count the number of times a value appears in an array?
Here's an example of this easy to use function so that you can see just how it works:
<?php
$values = array(1,1,1,2,2,2,2,3,3,4,2,5,1,3,5,3,1,5);
$most_popular = array_count_values($values);
print_r($most_popular);
?>
This prints the following:
(
[1] => 5
[2] => 5
[3] => 4
[4] => 1
[5] => 3
)
So you can see it returns an array with the values of the initial array as the keys of the new array, with the number of times each value was present in the initial array as the value in the new array.
Comment on this Question and Answer >>>
ASK A QUESTION
More arrays PHP Questions
How do I remove an element from an array without changing the index values for the rest of the array?when i fetch the record in the database but they skip first record in the database
How can I sort an array keeping the correlation between the index and value?
what is a array?
How can I reset an array in PHP?
