diff options
Diffstat (limited to 'sort_recursively.php')
-rw-r--r-- | sort_recursively.php | 40 |
1 files changed, 0 insertions, 40 deletions
diff --git a/sort_recursively.php b/sort_recursively.php deleted file mode 100644 index 4d089e1..0000000 --- a/sort_recursively.php +++ /dev/null @@ -1,40 +0,0 @@ -<?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); - } - } -} |