An quick fix, using the previous numerically ordered array, could have been:
// Save the keys
$keys = array_shift($data);
/* here, do the sorting ... */
// Then apply the keys to your ordered array
$data = array_map(function ($item) {
global $keys;
return array_combine($keys, $item);
}, $data);
But let’s update my previous function:
function mult_usort_assoc(&$arr, $max_index = false, $index = false) {
function mult_usort_callback($a, $b, $max_index, $index) {
$max_index = $max_index ?: key(end($a)); // If not provided, takes last key
$index = $index ?: array_keys($a)[0]; // If not provided, takes first key
// When data are equal, sort by next key only if not after the $max_index
if ($a[$index] == $b[$index]) {
$next = array_keys($a)[array_search($index,array_keys($a))+1];
if ($index !== $max_index && !is_null($next)) {
return mult_usort_callback($a, $b, $max_index, $next);
} else {
return 0;
}
}
return $a[$index] > $b[$index] ? 1 : -1;
}
/* This part have been improved as we shouldn't use create_function()
This function is now usable in PHP 7.2 */
usort($arr, function ($a, $b) use ($max_index, $index) {
return mult_usort_callback($a, $b, $max_index, $index);
});
}
Usage and output:
mult_usort_assoc($data, 'Site');
echo '<pre>' . print_r($data, true) . '</pre>';
Used functions in this answer:
2
solved How to sort multidimensional array in PHP version 5.4 – with keys?