我有一個php
對象,我想將其轉換爲以下json
格式。將php對象轉換爲json
所有data
值是通過在格式php
對象對象:
Array ([0] => stdClass Object ([id] => 2
[s1] => 1485
[name] => 1485
[credit] => ''
[caption] => ''
)
)
我child
下試圖組credit, caption
等。此外,我無法獲得date
格式的json
格式之後的[
。
{
"mydata":
{
"name":Name,
"type":"default",
"date": [
{
"s1":"1485",
"name":"1485",
"child":
{
"credit":"",
"caption":""
}
}]
}
}
目前,我的代碼如下所示:
foreach ($hits as $hit) {
$data->mydata->date->s1 = $hit->s1;
$data->mydata->date->name = $hit->name;
$data->mydata->date->credit = $hit->credit;
$data->mydata->date->caption = $hit->caption;
}
$a = json_encode($data);