If I have this array,
ini_set('display_errors', true);
error_reporting(E_ALL);
$arr = array(
'id' => 1234,
'name' => 'Jack',
'email' => 'jack@example.com',
'city' => array(
'id' => 55,
'name' => 'Los Angeles',
'country' => array(
'id' => 77,
'name' => 'USA',
),
),
);
I can get the country name with
$name = $arr['city']['country']['name'];
But if the country array doesn't exist, PHP will generate warning:
Notice: Undefined index ... on line xxx
Sure I can do the test first:
if (isset($arr['city']['country']['name'])) {
$name = $arr['city']['country']['name'];
} else {
$name = ''; // or set to default value;
}
But that is inefficient. What is the best way to get $arr['city']['country']['name']
without generating PHP Notice if it doesn't exist?