我很难使用下面的PHP代码创建以下JSON格式。请建议。
$postArray = array(
"MainPage" => array(
"i_date"=> "10-10-2012",
"i_location"=>$_POST["location-text"],
"i_summary"=>$_POST["summary-text"],
"SubPage" => "[" array(
"first_name"=> $_POST["first-1"],
"last_name"=>$_POST["last-1"]
),
array(
"first_name"=>$_POST["first-2"],
"last_name"=>$_POST["last-2"]
) "]"
)
);
array_push($postArray["MainPage"]["SubPage"],
array(
"first_name"=>$_POST["first-3"],
"last_name"=>$_POST["last-3"]
)
);
$json = json_encode( $postArray );
这是我想要实现的最终输出。
{
"MainPage":{
"i_date":"2014-03-20",
"i_location":"test",
"i_summary":"test test",
"SubPage":[
{
"first_name":"first name test1",
"last_name":"last name test1"
},
{
"first_name":"first name test2",
"last_name":"last name test2"
},
{
"first_name":"first name test3",
"last_name":"last name test3"
}
]
}
}
答案 0 :(得分:2)
我对原始代码进行了一些更改,这些更改可以提供您想要的结果。
$postArray = array(
'MainPage' => array(
'i_date' => '2014-03-20',
'i_location' => $_POST["location-text"],
'i_summary' => $_POST["summary-text"],
'SubPage' => array(
array(
'first_name' => $_POST["first-1"],
'last_name' => $_POST["last-1"]
),
array(
'first_name' => $_POST["first-2"],
'last_name' => $_POST["last-2"]
)
)
)
);
$postArray['MainPage']['SubPage'][] = array(
'first_name' => $_POST["first-3"],
'last_name' => $_POST["last-3"]
);
$json = json_encode($postArray);