php输出json数据,PHP API接口必备之输出json格式数据示例代码
时间: 2024-03-14 21:48:45 浏览: 103
下面是一个简单的 PHP 输出 JSON 格式数据的示例代码:
<?php
header('Content-Type: application/json');
$data = array(
'name' => 'John Doe',
'age' => 35,
'email' => 'john.doe@example.com'
);
echo json_encode($data);
?>
在这个示例代码中,我们首先设置了 HTTP 响应头的 Content-Type 为 application/json,表示输出的是 JSON 格式数据。然后,我们定义了一个包含 name、age 和 email 三个属性的数组 $data。最后,我们使用 json_encode() 函数将 $data 数组转换为 JSON 格式数据,并使用 echo 输出。
阅读全文
相关推荐

















