我希望我的$ row->响应对象看起来像这样:
"responses": [
{
"id": 1381822,
"user_id": 45313,
"respondent_name": "JP Pullen",
"response_time": 1424127673,
"is_root_response": true,
"response_tree_id": 1377018,
"project_users_id": 74311,
"display_name": "Mobile 2",
"lft": 1,
"rgt": 2,
"response_read": true,
"body": "Sure it really is was great I purchased choclate at my local grocery store.",
"answers": [ ],
"avatar_url": null,
"mtime": null,
"media_list": [ ]
},
然而,我的$ row->回复对象如下所示:
"responses": {
"1": {
"id": 1381825,
"user_id": 45167,
"respondent_name": "First Name Last Name",
"response_time": 1424128177,
"is_root_response": true,
"response_tree_id": 1377021,
"project_users_id": 74312,
"display_name": "SimonSays",
"lft": 3,
"rgt": 4,
"response_read": false,
"body": "What's up!",
"answers": [ ],
"avatar_url": "https:\/\/portalvhds5kcv8nfhdz8zn.blob.core.windows.net\/user-45167\/avatar-50x50",
"mtime": 1420206441,
"media_list": [ ]
}
}
如何摆脱对象中的愚蠢{1}。我没有得到它,直到我不得不循环通过对象并取消其中一个。我不是PHP专家。强类型FTW lol。
foreach($row->responses as $elementKey => $element)
^这就是我通过响应对象循环来取消设置一个:
unset($row->responses{$elementKey});
答案 0 :(得分:1)
当使用json_encode()
编码数组时,PHP将确定它是否应该使用数组语法(方括号)或对象语法(花括号)。在这种情况下,第一个数组索引是1而不是0,因此无法可靠地编码为JavaScript数组。
为了防止这种情况发生,您可以使用array_values()
将数组重新编号为0:
$row->responses = array_values($row->responses);