How can PHP be used to sort an array retrieved from a SQL query without losing the previously selected data?

When retrieving an array from a SQL query in PHP, you can sort the data without losing the previously selected information by using the array_multisort() function. This function allows you to sort multiple arrays at once while maintaining the key associations. By specifying the sorting order and the array you want to sort, you can rearrange the data without losing any of the original information.

// Assume $data is the array retrieved from the SQL query
// Sort the array by a specific key while maintaining the original data
$keys = array_column($data, 'key_to_sort');
array_multisort($keys, SORT_ASC, $data);

// Now $data is sorted by the 'key_to_sort' field without losing any of the previously selected data