I currently have this code for populating a list of comments to return:
foreach ($comments as $comment)
{
$f_comments[] = array (
'comment_id' => $comment['comment_id'],
'comment_body' => $comment['comment_body'],
'user_id' => $comment['user_id'],
'user_avatar' => $comment['user_avatar'],
'user_username' => $comment['user_username'],
'timeago' => formatter_common_format_time_string($comment['added']),
);
if (!empty($comment['user_picture']))
{
$f_comments[]['user_picture'] = $comment['user_picture'];
}
}
The problem is, this is what gets returned:
"comments":[{"comment_id":9386,"comment_body":"Comment","user_id":46542,"user_avatar":"9","user_username":"TestUser","timeago":"about 2 hours ago"},{"user_picture":"ec237f517bc26b27d8e790c3a5d125841321552075"}]
…whereas I want user_picture in with the rest of the results, like this:
[{"comment_id":9386,"comment_body":"Comment","user_id":46542,"user_avatar":"9","user_username":"TestUser","timeago":"about 2 hours ago", "user_picture":"ec237f517bc26b27d8e790c3a5d125841321552075"}]
…however I can’t see a way of doing this. I’m quite new to PHP so I’m probably missing something obvious. Can anyone see what’s wrong?
Add the index your comment:
Or just create a variable $i = 0; before the foreach an increase it before you close it $i++;