What is the correct way to read and display specific parts of a JSON file using PHP?

To read and display specific parts of a JSON file using PHP, you can use the json_decode() function to decode the JSON file into a PHP array. You can then access specific parts of the array using array indexing or looping through the array elements. Finally, you can display the specific parts using echo or print statements.

<?php
// Read the JSON file
$json_data = file_get_contents('data.json');

// Decode the JSON data into a PHP array
$data = json_decode($json_data, true);

// Access and display specific parts of the array
echo $data['key1']['nested_key']; // Display a specific nested key
foreach($data['key2'] as $item) {
    echo $item['name']; // Display a specific value from a list of items
}
?>