Search code examples
phparrayscountunique

Count occurrences of unique values from a flat array


I need to count the occurrences of all of the unique values from my array. Here is an example of it:

Array
(
    [0] => 1
    [1] => 1
    [2] => 1
    [3] => 1
    [4] => 4
    [5] => 6
    [6] => 6
)

I'm looking for a way to sum all from same value:

Array
(
    [1] => 4
    [4] => 1
    [6] => 2
)

Solution

  • RTM: http://www.php.net/array_count_values

    ps: assuming that "[5] => 5" is a typo. Otherwise explain more carefully.