hellomello
hellomello

Reputation: 8585

push certain key/value into existing array

I'm trying to put data into 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
                    )
            )
    )

I have something like this:

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

But it doesn't fall into the from. Not sure what the right syntax is to get $SQL_comments_from into data such like the example above.

This is what I'm getting

[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
                            )
                    )
            )
    )

I'm trying to get "from" into the "data". The example I provided does not make the "from" go into the other "from" (thats right under idcomments)

Thanks in advance!

Upvotes: 0

Views: 415

Answers (1)

Musa
Musa

Reputation: 97672

Your data field uses data from SQL_comments_from and $SQL_comments_from mixed together, this should generate what you want.

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

}                           

Upvotes: 1

Related Questions