0

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!

1 Answer 1

1

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

}                           
Sign up to request clarification or add additional context in comments.

4 Comments

What do you mean it mixed together?
Thanks Musa, but is there another way instead of getting just $from[0], what if I have multiple arrays? Like I'm matching id's would $from[0] be changed to something else? Thanks!
I thought that but since $from only had one item in it I used $from[0]
thanks, that got me in the right direction. What I had to do was add $i = 0; before the foreach loop, then did $from[$i] and have a $i++ right before the foreach loop ended. This way, it will iterate through the array. I did it this way because my while loop already sets the matching, so essentially, it just needs to go down the list in order, from beginning to end. makes sense? maybe there's another better way, but this one works! Thanks !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.