How can an associative array be sorted using ksort in PHP?

To sort an associative array using ksort in PHP, you can use the ksort() function which sorts the array by its keys in ascending order. This function will rearrange the elements of the array so that they are sorted based on their keys. This can be useful when you need to display the array in a specific order or perform operations that require a sorted array.

$assoc_array = array("b" => 2, "a" => 1, "c" => 3);
ksort($assoc_array);
foreach ($assoc_array as $key => $value) {
    echo $key . " => " . $value . "\n";
}