PHP - count frequency of array values

arrays, count, frequency, php

Solution

Sort them after counting them with `arsort()`

$result = array_count_values(explode(',', $array));
arsort($result);

Array
(
    [joe] => 4
    [1] => 3
    [2] => 2
    [4] => 2
    [3] => 2
    [9] => 1
    [8] => 1
    [5] => 1
    [6] => 1
    [7] => 1
)

Problem

Is there a way in php to count how often a value exists in a large array? So if I have an array like this: ``` $array = "1,2,3,4,joe,1,2,3,joe,joe,4,5,1,6,7,8,9,joe"; ``` is there a way to output a new array that tells me (and sorts) which is used most and how many for each? ``` $result = array( [joe] => 4 [1] => 3 [2] =>2 etc... ) ``` I've seen the php array_count_values, but can this be sorted by most -> least? or is there an easier way? Thanks everyone!

Original source