What potential pitfalls should be considered when sorting a multidimensional array in PHP?

When sorting a multidimensional array in PHP, one potential pitfall to consider is that the sorting function may not work as expected if the array keys are not preserved during the sorting process. To avoid this issue, you can use the uasort() function, which sorts the array with a user-defined comparison function while maintaining the original keys.

// Example of sorting a multidimensional array while preserving keys
$multiArray = [
    ['name' => 'John', 'age' => 25],
    ['name' => 'Alice', 'age' => 30],
    ['name' => 'Bob', 'age' => 20]
];

// Custom sorting function based on 'age'
function sortByAge($a, $b) {
    return $a['age'] <=> $b['age'];
}

// Sort the multidimensional array using uasort()
uasort($multiArray, 'sortByAge');

// Output the sorted array
print_r($multiArray);