What are some common pitfalls when using the sort function in PHP for multidimensional arrays?

One common pitfall when using the sort function in PHP for multidimensional arrays is that it only sorts the first level of the array, leaving the inner arrays unaffected. To solve this, you can use a custom sorting function with array_multisort to sort the inner arrays as well.

// Sample multidimensional array
$students = array(
    array("name" => "Alice", "age" => 20),
    array("name" => "Bob", "age" => 22),
    array("name" => "Charlie", "age" => 21)
);

// Custom sorting function to sort by age
function custom_sort($a, $b) {
    return $a['age'] - $b['age'];
}

// Sort the array by age
usort($students, 'custom_sort');

// Output sorted array
print_r($students);