What are some best practices for handling array indices in PHP?

When working with array indices in PHP, it is important to ensure that you are accessing valid indices to prevent errors such as "Undefined offset" or "Array index out of bounds". One best practice is to always check if an index exists before trying to access it using functions like isset() or array_key_exists(). Another good practice is to use built-in array functions like array_key_first() and array_key_last() to get the first and last keys of an array.

// Check if an index exists before accessing it
if (isset($array[$index])) {
    $value = $array[$index];
    // do something with $value
}

// Using array_key_first() and array_key_last() to get first and last keys
$firstKey = array_key_first($array);
$lastKey = array_key_last($array);