What are the best practices for accessing nested arrays and objects in PHP when working with JSON data?

When working with JSON data in PHP, it is common to encounter nested arrays and objects. To access values within these nested structures, you can use a combination of array and object notation. For arrays, you can simply use square brackets with the key to access the value. For objects, you can use the arrow operator (->) followed by the property name. By understanding how to navigate nested arrays and objects, you can effectively work with JSON data in PHP.

// Sample JSON data
$jsonData = '{"name": "John Doe", "age": 30, "address": {"street": "123 Main St", "city": "New York"}}';

// Decode the JSON data into an associative array
$data = json_decode($jsonData, true);

// Accessing nested values
$name = $data['name'];
$city = $data['address']['city'];

echo $name; // Output: John Doe
echo $city; // Output: New York