<?php
class comparer {
public static array $counter = [];
public static function compare($a, $b) {
static::$counter[$a] = (static::$counter[$a] ?? 0) + 1;
static::$counter[$b] = (static::$counter[$b] ?? 0) + 1;
return $a <=> $b;
}
}
$a=[5,6,7,8,9,10,1,2,3,4];
uasort($a, 'comparer::compare');
var_dump(comparer::$counter);
- Output for 8.1.32, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
- array(10) {
[5]=>
int(5)
[6]=>
int(6)
[7]=>
int(2)
[8]=>
int(6)
[9]=>
int(2)
[10]=>
int(5)
[1]=>
int(5)
[2]=>
int(6)
[3]=>
int(6)
[4]=>
int(5)
}
preferences:
56.79 ms | 406 KiB | 5 Q