Is there a way to dynamically determine whether to access an item as a stdClass or an array in PHP?

When working with data that can be either an object (stdClass) or an array in PHP, it can be challenging to dynamically determine the correct way to access its properties. One way to solve this issue is to check the type of the variable and then access its properties accordingly. By using the `is_array()` function, we can determine if the variable is an array and access its elements using square brackets. If the variable is an object, we can access its properties using the arrow operator (->).

$data = json_decode($json_data);

if (is_array($data)) {
    $value = $data['key'];
} else {
    $value = $data->key;
}

echo $value;