What are common pitfalls when trying to sort multi-dimensional arrays in PHP?
One common pitfall when sorting multi-dimensional arrays in PHP is not specifying the sorting criteria correctly. When sorting multi-dimensional arrays, you need to use a custom sorting function that compares the specific values you want to sort by. Another pitfall is not using the correct sorting function for multi-dimensional arrays, as the built-in sorting functions in PHP may not work as expected.
// Example of correctly sorting a multi-dimensional array by a specific key
$multiDimArray = [
['name' => 'John', 'age' => 30],
['name' => 'Alice', 'age' => 25],
['name' => 'Bob', 'age' => 35]
];
usort($multiDimArray, function($a, $b) {
return $a['age'] - $b['age'];
});
print_r($multiDimArray);