2017-04-23 52 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