I need to count the occurrences of each element in a given array. So far, I have managed to achieve this with the exception that it loops all elements and I need to output only those which are unique.
<?php
$array = array('b','b','a','a','b');
foreach ($array as $element) {
$hold = implode(" ", $array);
print $element. ' -> ' . substr_count($hold, $element) . '<br />';
}
Thus the output now is:
b -> 3
b -> 3
a -> 2
a -> 2
b -> 3
I want to remove the duplicate elements but if I do array_unique() before I implode() and subsequently make the count, I will get only one occurrence due to the fact that I have removed the repeating elements.