In the context of PHP programming, how can you implement custom sorting logic for arrays that involve nested levels of keys and values?

When dealing with arrays that have nested levels of keys and values, you can implement custom sorting logic by using a recursive function that traverses through the array and sorts it based on your custom criteria. This function should be able to handle both associative and indexed arrays at any depth.

<?php
function customArraySort(&$array, $sortBy) {
    if (is_array($array)) {
        foreach ($array as $key => $value) {
            customArraySort($array[$key], $sortBy);
        }
        
        if (is_array($array) && !empty($sortBy)) {
            uasort($array, function($a, $b) use ($sortBy) {
                return $a[$sortBy] <=> $b[$sortBy];
            });
        }
    }
}

$array = [
    ['name' => 'John', 'age' => 30],
    ['name' => 'Alice', 'age' => 25],
    ['name' => 'Bob', 'age' => 35]
];

customArraySort($array, 'age');

print_r($array);
?>