Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sort results of array count values into a new array

I want to sort the results to a new array but without the $key so the most UNunique number will be first (or the most duplicated number will be first).

<?php

$a = array (1,1,2,2,2,3,3,3,3,4,4,4,4,4,5);

foreach (array_count_values($a) as $key => $value) {
        echo $key.' - '.$value.'<br>'; 
}

//I am expecting to get the most duplicated number FIRST (without the $key)
//so in that case :
// $newarray = array(4,3,2,1,5); 
?>

1 Answers

$a = array (1,1,2,2,2,3,3,3,3,4,4,4,4,4,5);

$totals = array_count_values($a);

arsort( $totals );

echo "<pre>";
print_r($totals);

Output

Array
(
    [4] => 5
    [3] => 4
    [2] => 3
    [1] => 2
    [5] => 1
)
like image 155
Goran Usljebrka Avatar answered Nov 08 '25 12:11

Goran Usljebrka