将某个键/值推入现有数组

时间:2012-07-03 02:48:45

标签: php arrays multidimensional-array push

我正在尝试将数据放入from

[comments] => Array
    (
        [count] => 2
        [data] => Array
            (
                [0] => Array
                    (
                        [idcomments] => 1
                        [from] => Array
                               (
                                    [idusers] => 1
                                    [username] => 
                                    [full_name] => AndrewLiu
                               )
                        [text] => testing this comment out
                   )
                [1] => Array
                    (
                        [idcomments] => 2
                        [from] => Array
                            (
                                [idusers] => 1
                                [username] => 
                                [full_name] => AndrewLiu
                            )
                        [text] => more comments yeah
                    )
            )
    )

我有这样的事情:

while($row = $SQL_products -> fetch(PDO::FETCH_ASSOC)){
    $json['data'][] = array(
        "comments" =>array(
            "count"=>$SQL_ccount[0],
            "data" => array($SQL_comments->fetchAll(PDO::FETCH_ASSOC),
            "from" => array($SQL_comments_from->fetchAll(PDO::FETCH_ASSOC)))
        ),
    );
}

但它不属于from。不确定将$SQL_comments_from引入data的正确语法是什么,例如上面的示例。

这就是我得到的

[comments] => Array
    (
        [count] => 2
        [data] => Array
            (
                [0] => Array
                    (
                        [0] => Array
                            (
                                [idcomments] => 1
                                [from] => 1
                                [text] => testing this comment out
                            )
                        [1] => Array
                            (
                                [idcomments] => 2
                                [from] => 2
                                [text] => more comments yeah
                            )
                    )
                [from] => Array
                    (
                        [0] => Array
                            (
                                [idusers] => 1
                                [username] => 
                                [full_name] => AndrewLiu
                            )
                    )
            )
    )

我正试图将"from"加入"data"。我提供的示例并没有使“from”进入另一个“from”(就在idcomments下面)

提前致谢!

1 个答案:

答案 0 :(得分:1)

您的数据字段使用来自SQL_comments_from$SQL_comments_from的数据混合在一起,这应该可以生成您想要的内容。

while($row = $SQL_products -> fetch(PDO::FETCH_ASSOC)){
    $from = $SQL_comments_from->fetchAll(PDO::FETCH_ASSOC);
    $data= array();
    foreach ($SQL_comments->fetchAll(PDO::FETCH_ASSOC as $items){
        $data[] = array('idcomments' => $items['idcomments'], 'from' => $from[$items['from']], 'text' => $items['text']);
    }
    $json['comments'][] = array(
            "count"=>$SQL_ccount[0],
            "data" => $data;
            )
        ),
    );

}