What are the potential pitfalls of using array_multisort() in PHP for sorting complex data structures?

One potential pitfall of using array_multisort() in PHP for sorting complex data structures is that it can be challenging to maintain and understand the sorting logic when dealing with multiple arrays. To solve this issue, you can create a custom sorting function that encapsulates the sorting logic for the complex data structure.

// Define a custom sorting function for complex data structures
function customSort($data) {
    $sortKeys = array_column($data, 'key_to_sort_by');
    array_multisort($sortKeys, SORT_ASC, $data);
    return $data;
}

// Example usage
$complexData = [
    ['key_to_sort_by' => 3, 'other_data' => 'foo'],
    ['key_to_sort_by' => 1, 'other_data' => 'bar'],
    ['key_to_sort_by' => 2, 'other_data' => 'baz']
];

$sortedData = customSort($complexData);
print_r($sortedData);