我目前在创建json响应时遇到问题,因为我没有那么多使用PHP,但任何人都可以帮我创建json,如下所示:
{
"set_attributes":
{
"apikey": "some value",
},
"block_names": ["getdata"],
"type": "show_block",
"title": "go"
}
这是我的代码:
$response = array();
$response['block_names'] = array();
$response['block_names'] = "getdata";
$response['type'] = "show_block";
$response['title'] = "go";
if ($db->studentLogin($username, $password)) {
$student = $db->getStudent($username);
$temp = array();
$temp['apikey'] = $student['api_key'];
$response['set_attributes'] = $temp['apikey'];
} else {
$temp = array();
$response['messages'] = array();
$temp['text'] = "Invalid username or password";
array_push($response['messages'],$temp);
}
echoResponse(200, $response);
它显示如下:
{
"block_names": "getdata",
"type": "show_block",
"title": "go",
"set_attributes": "f74911b29778adea86aa24d5ce85ff58"
}
但我想添加apikey =" f74911b29778adea86aa24d5ce85ff58"在set_attributes和[]之外的block_names就像obove一样。我怎么能这样做?
答案 0 :(得分:1)
试试这个:
$response['block_names'] = array("getdata");
...
$response['set_attributes'] = new \stdClass();
$response['set_attributes']->apikey = 'some_api_key';