blob: 4d089e1c1a90302e5c232b4986118116f9b90098 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
<?php
/**
* Comparison function for custom sort. Tries to achieve a canonical sort order across PHP versions
* by sorting primarily by type, secondarily by value.
* @param mixed $a
* @param mixed $b
*
* @return int
*/
function byTypeAndValue($a, $b): int
{
if (gettype($a) !== gettype($b)) {
return gettype($a) <=> gettype($b);
} else {
return $a <=> $b;
}
}
/**
* Sort an array by its values, recursively
* @param array &$array
*/
function sortRecursively(array &$array): void
{
// Sequential array, re-index after sorting
if (array_keys($array) === range(0, count($array) - 1)) {
usort($array, 'byTypeAndValue');
}
// Associative array, maintain keys
else {
uasort($array, 'byTypeAndValue');
}
foreach ($array as &$value) {
if (is_array($value)) {
sortRecursively($value);
}
}
}
|