What I'm doing is this:
array_count_values. this outputs ([1]=>1, [2]=>2, ...)array_keys($array) to get the IDs in a count ordered list.I once used array_multisort for a similar function but in that case the keys were strings ('a'=>2). The problem now is that I'm using numeric keys and multisort re-indexes the keys to 1, 2, 3 because the keys holding the count value are numeric IDs. This of course screws the purpose 'cause I can't identify anything anymore..
Anyway, here's what I'm roughly doing now:
$array = array(3, 1, 2, 3, 2, 3);
// count the IDs [0]=>3, [1]=>1, [2]=>2
$count = array_count_values($array);
// sort and screw up the id's: [0]=>3 [1]=>1 [2]=>2
array_multisort($count);
Something tells me that there's a better way of approaching this?
Try asort instead of array_multisort as it maintains the original index association.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With