<?php
// Everything you enter here will be executed by our servers. Try it!
$arr = array(10,8,13,8,5,10,10,12);
$occurrences = array_count_values($arr);
usort($arr, function ($a, $b) use ($occurrences) {
// if the occurrence is equal (== 0), return value difference instead
return ($occurrences[$a] - $occurrences[$b]) ?: ($a - $b);
});
print_r($arr);