What are the potential pitfalls or issues that can arise when trying to sort a multidimensional array in PHP based on multiple fields?

When sorting a multidimensional array in PHP based on multiple fields, one potential pitfall is that the built-in sorting functions may not support sorting by multiple fields simultaneously. To solve this issue, you can use a custom sorting function that compares the values of each field one by one.

<?php
// Sample multidimensional array
$data = [
    ['name' => 'John', 'age' => 30],
    ['name' => 'Jane', 'age' => 25],
    ['name' => 'Alice', 'age' => 35],
];

// Custom sorting function based on 'name' and 'age' fields
usort($data, function($a, $b) {
    if ($a['name'] == $b['name']) {
        return $a['age'] - $b['age'];
    }
    return $a['name'] < $b['name'] ? -1 : 1;
});

// Output sorted array
print_r($data);
?>