0

我正在尝试将数据放入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". 我提供的示例不会使“发件人”进入另一个“发件人”(就在 idcomments 下)

提前致谢!

4

1 回答 1

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;
            )
        ),
    );

}                           
于 2012-07-03T03:23:55.733 回答