doupoji3856 2012-01-10 01:27 采纳率: 100%
浏览 247
已采纳

使用两个mySQL查询来创建嵌套的JSON数组

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条回答 默认 最新

  • douwo1517 2012-01-10 01:31
    关注

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。