What are the potential pitfalls of sorting data by multiple columns in PHP?

When sorting data by multiple columns in PHP, one potential pitfall is that the sorting may not work as expected if the data types of the columns are not compatible. To solve this issue, you can use a custom sorting function that compares the values of the columns appropriately.

// Example of sorting data by multiple columns with a custom sorting function
$data = [
    ['name' => 'Alice', 'age' => 30],
    ['name' => 'Bob', 'age' => 25],
    ['name' => 'Alice', 'age' => 25],
];

usort($data, function($a, $b) {
    if ($a['name'] == $b['name']) {
        return $a['age'] - $b['age'];
    }
    return strcmp($a['name'], $b['name']);
});

print_r($data);