Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7039381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:47:29+00:00 2026-05-28T01:47:29+00:00

I am trying to create a nested JSON array with the results of the

  • 0

I am trying to create a nested JSON array with the results of the second query being attached to the rows of the first.

My code so far is as follows:-

$output = array();

$sql = "select cp_comments.*,users.user_login from ".$wpdb->prefix."cp_comments cp_comments
                left join ".$wpdb->prefix."users users on users.ID=cp_comments.uid
                where songid='$id'
                order by cp_comments.id asc";               
    $comments = $wpdb->get_results($sql);



    foreach($comments as $c){


        $sql = "select cp_replies.*,users.user_login from ".$wpdb->prefix."cp_replies cp_replies
                    left join ".$wpdb->prefix."users users on users.ID=cp_replies.uid
                    where cp_replies.cid='".$c->id."'
                    order by cp_replies.id asc";
        $replies = $wpdb->get_results($sql);


    $output['comment-'.$c->id] = $c;        


        if($replies){

            foreach($replies as $r){

            // The line below causes the problem
            //$output['comment-'.$c->id][] = $r;


            }       
        }           
    }

    echo json_encode( $output );

As you can see what I am attempting to do is retrieve the results of query-1, lop through them and populate an array. So far so good. Then for each row in the resultset, perform a second query, using $c->id as a variable to dynamically change the second query dependant upon the id, then nest this data within each returned row of the first query.

The line I have commented out causes an error:-

Fatal error: Cannot use object of type stdClass as array in

Although I have a rough understanding of why this error is happening, I do not know how to fix it and when I have tried a standard multidimensional array using while loops etc, I was then not able to access the $c->$id variable making the whole second query worthless.

Essentially I would like the data returned in this format:-

{ "comment-2" : [ { "avatar" : "http://www.songbanc.com/wp-content/uploads/avatars/1/8bb11e958a26913e2c13393014e854d5-bpthumb.jpg",
        "body" : "More tests....",
        "display_name" : "admin",
        "id" : "26",
        "playtime" : 36.206896551699998,
        "posttime" : "2011-10-08 11:11:55",
        "cid" : "26",
        "songid" : "30",
        "uid" : "1",
        "user_login" : "admin",
        "user_url" : "http://www.songbanc.com/members/admin/"
        "cid": "1",
        "replies" : [ { "cid" : "26",
                        "body" : "test reply",
                        "posttime" : "2011-10-08 11:11:55"
                      }]

      },

It is currently 2 dimensional however without the ‘replies’.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:47:30+00:00Added an answer on May 28, 2026 at 1:47 am

    $output[‘comment-‘.$c->id] is an object. I think you want something like

    $output[‘comment-‘.$c->id]->replies[] = $r;

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create nested array's dynamically from a string that has been
I am trying to create a 2d array using nested containers. I want to
I am trying to create a nested form using formtastic. I've included my code
I am trying to create a nested JSON that has data from various db
I’m trying to create an instance of: java.awt.geom.Point2D.Double in ColdFusion. Point2D.Double is a nested
Trying to create my first iPhone app that would play back audio. When I
I am trying to create nested resources where the sub-resource doesn't have an existence
Background / Goal More or less, I'm trying to create a nested group of
I am trying to create a TreeView nested structure with the use of self
I'm trying to create a perma link for a nested attribute. For example, look

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.