What are the potential pitfalls of sorting multidimensional arrays in PHP when combining results from different queries?

When combining results from different queries into a multidimensional array in PHP, a potential pitfall is that the arrays may have different structures or keys, making it challenging to sort them together. One solution is to standardize the structure of the arrays before sorting by ensuring they have the same keys or structure.

// Example code snippet to standardize the structure of multidimensional arrays before sorting

// Sample arrays from different queries
$array1 = [
    ['id' => 1, 'name' => 'Alice', 'age' => 25],
    ['id' => 2, 'name' => 'Bob', 'age' => 30]
];

$array2 = [
    ['id' => 3, 'name' => 'Charlie', 'age' => 28],
    ['id' => 4, 'name' => 'David', 'age' => 35]
];

// Standardizing the structure of arrays by adding a common key
foreach ($array1 as &$item) {
    $item['source'] = 'query1';
}

foreach ($array2 as &$item) {
    $item['source'] = 'query2';
}

// Combining arrays
$combinedArray = array_merge($array1, $array2);

// Sorting the combined array by 'age'
usort($combinedArray, function($a, $b) {
    return $a['age'] - $b['age'];
});

// Output the sorted combined array
print_r($combinedArray);