이것이 간단 할 수 있다는 것을 알고 있지만 막혀서 해결책을 찾을 수 없습니다. 다음과 같은 간단한 중첩 json 객체를 만들고 싶습니다.
{
"user": {"firstname":"foo","lastname":"bar","email":"[email protected]"}
}
지금까지 다음과 같이 내부 json을 만들 수 있습니다.
class user_profile {
private $firstname = '';
private $lastname ='';
private $email = '';
public function __construct($first, $last, $email){
$this->firstname = $first;
$this->lastname = $last;
$this->email = $email;
}
public function expose() {
return get_object_vars($this);
}
}
$up = new user_profile('foo','bar','[email protected]');
echo json_encode($up->expose());
배열 추가를 시도했습니다.
echo json_encode(array('user',$up->expose()), JSON_FORCE_OBJECT);
그러나 그 결과 :
{
"0":"user","1": {"firstname":"foo","lastname":"bar","email":"[email protected]"}
}
외부 "사용자"부분을 어떻게 만듭니 까?
넌 할 수있어 json_encode(array('user' => $up->expose());
그래서 기본적으로 당신이 저지른 유일한 실수는 키 => 값을 가진 연관 배열이 아니라 두 개의 객체가있는 배열을 제공 하는 ,
대신을 사용하는 것입니다 =>
.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다