How can nested data structures, such as questions and answers, be properly formatted in JSON output in PHP?

Nested data structures, such as questions and answers, can be properly formatted in JSON output in PHP by creating an associative array where the questions are keys and the answers are values. This allows for easy serialization of the data into JSON format using the json_encode function in PHP.

$data = array(
    "question1" => "answer1",
    "question2" => "answer2",
    "question3" => "answer3"
);

$json_output = json_encode($data);
echo $json_output;