A Blog Of Random Coding Techniques & Experiments Robert Askam

PHP - How to count number of items in a list Post PHP - How to count number of items in a list 1 year ago

Here is a way to sort a list by value and then print it out the count of duplicates.

$string = 'one, two, two, three, three, three, three, four, four, four, four, five, five, five, five, five';
$a = explode(',', $string );
$b = array_count_values($a);
arsort($b);
foreach ($b as $x => $x_value) {
echo $x;
echo $x_value;
}