How can one avoid the error "Fatal error: Cannot use string offset as an array" when working with arrays in PHP?

When working with arrays in PHP, the error "Fatal error: Cannot use string offset as an array" occurs when trying to access an element of a string as if it were an array. To avoid this error, make sure to check if the variable is an array before trying to access its elements. You can use the `is_array()` function to check if a variable is an array before accessing its elements.

// Check if the variable is an array before accessing its elements
if (is_array($array) && isset($array['key'])) {
    // Access the element of the array
    $value = $array['key'];
}