What are the potential pitfalls of using eval() function in PHP for accessing array elements?

Using the eval() function in PHP to access array elements can be dangerous as it allows for the execution of arbitrary code, opening up the possibility of code injection attacks. It is recommended to avoid using eval() for this purpose and instead use more secure methods such as direct array access or functions like isset() and array_key_exists().

// Using direct array access to retrieve array elements
$array = ['key' => 'value'];
$key = 'key';
$value = $array[$key];
echo $value;