What are the default behaviors of array keys in PHP arrays and how can this impact array functions like array_rand()?

By default, PHP arrays use numerical keys starting from 0 if keys are not specified explicitly. This can cause issues when using array functions like array_rand() because the function expects an array with consecutive numerical keys to work properly. To solve this issue, you can reindex the array using array_values() before using array_rand() to ensure that the keys are consecutive numerical values.

$array = ['a' => 'apple', 'b' => 'banana', 'c' => 'cherry'];
$array = array_values($array);
$randomKey = array_rand($array);
echo $array[$randomKey];