Join array to Json output

Asked

Viewed 69 times

0

Hello, I’m having doubts how could I join this 4 array’s with the relationship between them. I have the page hierarchy -> post -> comments -> reply. I need to associate the posts to the page, the comments to the post and the responses to the comments. The relay between them is contained in the key. I separate a select from the bd where I use a left Join to eliminate redundancies and perform other filters. But the intention is that it will end up in the endpoint all together in JSON.

    foreach ( $this->db as $key ) 
    {

        if ( strlen( $key->idpage ) > 0 )
        {
            $this->mentions_page[ $key->idpage ] = $key->pg_about;
            $this->page[$key->idpage]            = array( 'nome' => $key->pg_name, 'category' => $key->pg_category, 'link' => $key->pg_link, 'about' => $key->pg_about, 'avatar' => $key->pg_avatar );          
        }

        if ( strlen( $key->ps_message ) >0 )
        {
            $this->mentions_post[ $key->idpost ] =  $key->ps_message;
            $this->post[ $key->idpost ]          = array( 'idpage' => $key->ps_idpage, 'name' => $key->ps_message, 'date' => $key->ps_date, 'link' => $key->ps_link, 'picture' => $key->ps_picture );                 
        }

        if ( strlen( $key->cm_message ) > 0 )
        {
            $this->mentions_comment[ $key->idcomment ]     = $key->cm_message;  
            $this->author_comment[ $key->idcomment ]       = $key->cm_name_author;   
            $this->sentiment_comment_id[ $key->idcomment ] = $this->GenSentiment($key->cm_message);

            $this->comment[ $key->cm_idcomment ]           = array( 'idpost' => $key->cm_idpost, 'date' => $key->cm_date, 'link' => $key->cm_link, 'message' => $key->cm_message, 
                                                                    'user' => array( 'id' => $key->cm_id_author, 'avatar' => $key->cm_avatar_author, 'name' => $key->cm_name_author, 'gender' => '0') );
        }

        if ( strlen( $key->rp_message ) > 0 )
        {
            $this->mentions_reply[ $key->idreply ]       = $key->rp_message;
            $this->author_reply[ $key->idreply ]         = $key->rp_name_author;
            $this->sentiment_reply_id[ $key->idreply ]   = $this->GenSentiment($key->rp_message);

            $this->reply[ $key->rp_idreply ]             = array( 'idcomment' => $key->rp_idcomment, 'date' => $key->rp_date, 'link' => $key->rp_link, 'message' => $key->rp_message, 
                                                                'user' => array( 'id' => $key->rp_id_author, 'avatar' => $key->rp_avatar_author, 'name' => $key->rp_name_author, 'gender' => '0') );
        }

    }
  • I think the easiest way to do it would be 3 foreachs, one to bring the pages, one inside this to bring the posts and one inside the second to bring the comments

  • This logic I’m using, my difficulty is in putting it together. I am declaring a variable $output and inside the page foreach I am incrementing the array in the prefixed variable of an if comparing the values, hopefully of certain =]

  • Can’t bring everything organized into the query? If not, there’s probably some modeling flaw

  • Yes, I can bring in the query, what I seek and create a customizable cache system, I play all over fetch for a Ssion and filter from it my results, as I have many Insert tuples, I want to consult as little as possible the bd.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.