What are common pitfalls when sorting data in PHP arrays, especially when dealing with multiple values per element?

Common pitfalls when sorting data in PHP arrays, especially with multiple values per element, include incorrectly specifying the sort order, not considering the data types of the values being sorted, and not using a custom sorting function when needed. To solve these issues, make sure to use the correct sorting function for the desired order, consider the data types of the values being sorted, and create a custom sorting function if the default sorting methods don't meet your requirements.

// Example of sorting an array with multiple values per element
$users = [
    ['name' => 'Alice', 'age' => 30],
    ['name' => 'Bob', 'age' => 25],
    ['name' => 'Charlie', 'age' => 35]
];

// Sort the array by age in ascending order
usort($users, function($a, $b) {
    return $a['age'] <=> $b['age'];
});

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