What are the best practices for accessing and displaying data from an associative array in PHP?

When accessing and displaying data from an associative array in PHP, it is important to check if the key exists before trying to access it to avoid errors. One way to do this is by using the isset() function to check if the key is set in the array. Additionally, using foreach loop to iterate through the array and display the data is a common practice.

// Sample associative array
$data = array(
    'name' => 'John Doe',
    'age' => 30,
    'email' => 'johndoe@example.com'
);

// Check if key exists before accessing it
if(isset($data['name'])) {
    echo 'Name: ' . $data['name'] . '<br>';
}

if(isset($data['age'])) {
    echo 'Age: ' . $data['age'] . '<br>';
}

if(isset($data['email'])) {
    echo 'Email: ' . $data['email'] . '<br>';
}

// Using foreach loop to iterate through the array and display data
foreach($data as $key => $value) {
    echo $key . ': ' . $value . '<br>';
}