2017-04-23 43 views
0

我目前有问题产生,因为我不使用PHP的那么多,但任何人都可以帮助我建立JSON象下面这样的JSON响应:如何在php中添加对象内的对象?

{ 
    "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

'$回应[ 'set_attributes'] = $温度;' - 现在你需要正义价值 – splash58

+0

'$回应[ 'block_names'] =阵列(”的GetData“);' – splash58

回答

1

试试这个:

$response['block_names'] = array("getdata"); 
... 
$response['set_attributes'] = new \stdClass(); 
$response['set_attributes']->apikey = 'some_api_key'; 

https://3v4l.org/GDMKT#output