What are some common pitfalls to avoid when working with arrays in PHP, especially when retrieving and manipulating data from a database?

One common pitfall when working with arrays in PHP, especially when retrieving and manipulating data from a database, is not properly checking if an array key exists before accessing it. This can lead to errors or unexpected behavior if the key does not exist. To avoid this issue, always use isset() or array_key_exists() to check if a key exists before trying to access it.

// Check if array key exists before accessing it
if(isset($array['key'])) {
    // Access the array key safely
    $value = $array['key'];
    // Do something with $value
} else {
    // Handle the case where the key does not exist
    echo 'Key does not exist';
}