What is the method for outputting JSON in PHP?
Utilize the json_encode() function in PHP to convert data into JSON format and display it. Example code is shown below:
$data = array("name" => "John", "age" => 30, "city" => "New York");
$json = json_encode($data);
echo $json;
The code above converts the array $data to JSON format and outputs it. If you need to return the JSON data to the front-end page, you can use the header() function to set the Content-Type to application/json, and then directly output the JSON data. An example code is shown below:
$data = array("name" => "John", "age" => 30, "city" => "New York");
$json = json_encode($data);
header('Content-Type: application/json');
echo $json;