如何制作具有相同名称的PHP stdClass对象?
I'm working against an API that gives me an stdClass object that looks like this (actual data replaced)
"data": [
{
"type": "image",
"comments": {
"data": [
{
"created_time": "1346054211",
"text": "Omg haha that's a lot of squats",
"from": {},
"id": "267044541287419185"
},
{
"created_time": "1346054328",
"text": "Fit body",
"from": {},
"id": "267045517536841021"
},
]
},
"created_time": "1346049912",
},
How is it possible to create an stdClass object like "Comments" that have multiple sub fields all with same name but different data. When I try to create an stdClass looking like this my Comments section will only contain 1 input which is the final one in the while loop. So instead of applying to the bottom it's replacing the old data with the new one. How to fix this?
我正在使用一个API,它给出了一个看起来像这样的stdClass对象(替换了实际数据) p>
“data”:[
{
“type”:“image”,
“comments”:{
“data”:[
{
“ created_time“:”1346054211“,
”text“:”Omg haha,这是很多下蹲“,
”来自“:{},
”id“:”267044541287419185“
},
{
“created_time”:“1346054328”,
“text”:“Fit body”,
“from”:{},
“id”:“267045517536841021”
},
]
},
“created_time”:“1346049912”,
},
code> pre>
如何创建一个stdClass对象,如“Comments”,它有多个子字段都具有相同的 名称但不同的数据。 当我尝试创建一个看起来像这样的stdClass时,我的Comments部分将只包含1个输入,这是while循环中的最后一个输入。
因此,它不是应用于底层,而是用新的数据替换旧数据。
如何解决这个问题? p>
div>
"comments"
is an object with a key "data"
which is an array of objects. You cannot reuse the same key in any language, JSON, PHP, stdClass
or otherwise. You want to make an array of similar objects.
$comments = new stdClass;
$comments->data = array();
for ($i = 0; $i < 2; $i++) {
$comment = new stdClass;
$comment->text = 'Lorem ipsum...';
...
$comments->data[] = $comment;
}
var_dump($comments);