【问题标题】:push certain key/value into existing array将某些键/值推入现有数组
【发布时间】:2012-07-03 02:48:45
【问题描述】:

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

提前致谢!

【问题讨论】:

    标签: php arrays multidimensional-array push


    【解决方案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;
                )
            ),
        );
    
    }                           
    

    【讨论】:

    • 混合在一起是什么意思?
    • 感谢 Musa,但如果我有多个数组,是否有其他方法可以代替仅获取 $from[0]?就像我正在匹配 id 一样,$from[0] 会被更改为其他内容吗?谢谢!
    • 我是这么想的,但是因为 $from 只有一个项目,所以我使用了$from[0]
    • 谢谢,这让我找到了正确的方向。我必须做的是在 foreach 循环之前添加$i = 0;,然后在 foreach 循环结束之前添加$from[$i] 并有一个$i++。这样,它将遍历数组。我这样做是因为我的 while 循环已经设置了匹配,所以本质上,它只需要从头到尾按顺序排列。说得通?也许还有另一种更好的方法,但是这个方法有效!谢谢!!
    猜你喜欢
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 2019-03-16
    • 1970-01-01
    • 1970-01-01
    • 2013-07-30
    • 1970-01-01
    相关资源
    最近更新 更多