What is the purpose of using array_multisort() in PHP and how does it work?

The purpose of using array_multisort() in PHP is to sort multiple arrays or a multi-dimensional array based on one or more keys. This function allows you to sort arrays in ascending or descending order, as well as specify the sorting order for each key. It is useful when you need to sort data in a specific way across multiple arrays or a multi-dimensional array.

// Example of using array_multisort() to sort multiple arrays based on one of the arrays
$array1 = array(10, 5, 8, 2);
$array2 = array("apple", "banana", "orange", "grape");
array_multisort($array1, $array2);

// Output sorted arrays
print_r($array1); // Output: Array ( [0] => 2 [1] => 5 [2] => 8 [3] => 10 )
print_r($array2); // Output: Array ( [0] => grape [1] => banana [2] => orange [3] => apple )