Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在foreach中有条件地向数组添加元素_Php_Arrays - Fatal编程技术网

Php 在foreach中有条件地向数组添加元素

Php 在foreach中有条件地向数组添加元素,php,arrays,Php,Arrays,我目前有以下代码用于填充要返回的注释列表: foreach ($comments as $comment) { $f_comments[] = array ( 'comment_id' => $comment['comment_id'], 'comment_body' => $comment['comment_body'], 'user_id' => $comment['user_id'],

我目前有以下代码用于填充要返回的注释列表:

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'];
    }
}
问题是,返回的是:

"comments":[{"comment_id":9386,"comment_body":"Comment","user_id":46542,"user_avatar":"9","user_username":"TestUser","timeago":"about 2 hours ago"},{"user_picture":"ec237f517bc26b27d8e790c3a5d125841321552075"}]
…而我希望用户_图片包含其余结果,如下所示:

[{"comment_id":9386,"comment_body":"Comment","user_id":46542,"user_avatar":"9","user_username":"TestUser","timeago":"about 2 hours ago", "user_picture":"ec237f517bc26b27d8e790c3a5d125841321552075"}]

…但是我看不出有什么办法。我对PHP很陌生,所以我可能遗漏了一些明显的东西。有人知道怎么回事吗?

在评论中添加索引:

foreach ($comments as $i => $comment)
{
    $f_comments[$i] = 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[$i]['user_picture'] = $comment['user_picture'];
    }
}

或者只创建一个变量$i=0在foreach之前增加它,然后再关闭它$i++

在评论中添加索引:

foreach ($comments as $i => $comment)
{
    $f_comments[$i] = 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[$i]['user_picture'] = $comment['user_picture'];
    }
}
或者只创建一个变量$i=0在foreach之前增加它,然后再关闭它$i++