What is the role of the array_diff function in comparing keys and values between arrays in PHP, and how can array_keys be utilized for key comparison?

The array_diff function in PHP is used to compare the values of two arrays and return the differences. If you want to compare the keys of two arrays instead, you can use the array_keys function to extract the keys from each array and then use array_diff to compare the keys.

$array1 = ['a' => 1, 'b' => 2, 'c' => 3];
$array2 = ['a' => 1, 'b' => 2, 'd' => 4];

$keys1 = array_keys($array1);
$keys2 = array_keys($array2);

$diff = array_diff($keys1, $keys2);

print_r($diff);