Recursively sort keys of a multidimensional array

Use a recursive function to call ksort on the current level and all deeper subarrays.

function recur_ksort(&$array) {
    foreach ($array as &$value) {
        if (is_array($value))
            recur_ksort($value);
     }
     ksort($array);
}

recur_ksort($array);
var_export($array);

Demo: https://3v4l.org/Xede5

Leave a Comment