我使用json编码制作了json代码,结果是正确的,但我想向对象添加satatus响应。如何?
这是我的代码
public function get(){
header('Content-Type: application/json');
$db = $this->M_order->db_order();
$response = array();
$data = array();
foreach ($db as $key) {
$data[] = array(
'id' => $key->id_user,
'name' => $key->name,
'destination' =>$key->destination
);
}
$response['data'] = $data;
echo json_encode($response, TRUE);
}
这个结果是我的json
{
"data": [
{
"id": "1",
"name": "amar",
"destination": "USA"
}
]
}
在这里,我想在对象中添加一个状态标头,如下所示。。。
{
"status": 200,
"error": false,
"data": [
{
"id": "1",
"name": "amar",
"destination": "USA"
},
]
}
如何创建?
我理解你的问题,
$response = array();
$data = array();
foreach ($db as $key) {
$data[] = array(
'id' => $key->id_user,
'name' => $key->name,
'destination' =>$key->destination
);
}
$response['status'] = 200;
$response['error'] = false;
$response['data'] = $data;