0

我的查询返回以下内容:

post_id    post_type    post_text
1          text         bla1
2          pic          bla2
3          text         bla3

我循环遍历这个结果如下:

$posts = array();
foreach ($query->result() as $row) {
    $post = array();
    $post['post_id']         = $row->post_id;
    $post['post_type']       = $row->post_type;
    $post['post_text']       = $row->post_text;
    $posts[] = $post;
}
$data['posts'] = $posts;

return $data;

如果我json_encode和我的控制器的输出我得到这个:

{
    "stream": {
        "posts": [{
            "post_id": "1",
            "post_type": "text",
            "post_text": "bla1",
        },
        {
            "post_id": "2",
            "post_type": "pic",
            "post_text": "bla2",
        },
        {
            "post_id": "3",
            "post_type": "text",
            "post_text": "bla3",            
        }]
    }
}

但是,如果帖子有评论,我的查询会返回

post_id    post_type    post_text    comment_id    comment_text
1          text         bla1         7             asd
1          text         bla1         8             sdf
2          pic          bla2
3          text         bla3         10            rty
3          text         bla3         11            yuo

我应该如何设置 foreach 循环来构建一个将像这样输出 JSON 的数组?

{
    "stream": {
        "posts": [{
            "post_id": "1",
            "post_type": "text",
            "post_text": "bla1",
            "comment": [{
                "comment_id": "7",
                "comment_text": "asd",
            },
            {
                "comment_id": "8",
                "comment_text": "sdf",
            }],
        },
        {
            "post_id": "2",
            "post_type": "pic",
            "post_text": "bla2",
        },
        {
            "post_id": "3",
            "post_type": "text",
            "post_text": "bla3",            
            "comment": [{
                "comment_id": "10",
                "comment_text": "rty",
            },
            {
                "comment_id": "11",
                "comment_text": "yuo",
            }],
        }]
    }
}

这个 JSON 在 Secha 框架上使用如下:

                    proxy: {
                        type: 'jsonp',
                        url: 'http://myurl',
                        reader: {
                            type: 'json',
                            rootProperty: 'stream.posts'
                        }
                    }
4

3 回答 3

2

尽管我更喜欢单独查询每个帖子是否存在评论,但这里将尝试使用一个查询来构造数组。

$posts=array();
$data = array();
foreach($query->result() as $row){
  // if the post hasn't been created in the array
  // create it
  if(!isset($posts[$row['post_id']])){
    $posts[$row['post_id']] = array(
      "post_type" => $row['post_type'],
      "post_text" => $row['post_text']
    );
  }
  // if a comment is found
  if(isset($row['comment_id'])){
    $posts[$row['post_id']]['comments'][] = array(
      "comment_id" => $row['comment_id'];
      "comment_text" => $row['comment_text'];
    );
  }

}
$data['posts'] = $posts;

JSON 编码这个数组会创建类似...

{ "posts" :
   {
     1 : {
       "post_type" : "something",
       "post_text" : "something else"
     },
     2 : {
       "post_type" : "something",
       "post_text" : "something else",
       "comments" : [
         {"comment_id":1,"comment_text":"something"},
         {"comment_id":2,"comment_text":"something"}
       ]
     },
    ....
   }
 }

我不能保证这段代码完全可以工作,因为我没有测试过它,但它应该是一个开始。同样,如果我是你,我只会单独查询每个帖子。

于 2012-09-17T01:58:46.913 回答
0

我不知道 PHP 语法,所以我不会编写代码。然而,这不是 PHP 问题,而是算法问题。

您可以分三个步骤处理这些数据,中间结果将是一个帖子字典。在这本字典中,键是帖子 ID,值是帖子。

1/ 遍历查询返回的行。对于每一行,检查该行所涉及的帖子是否存在于字典中。否 -> 添加一个新的帖子对象。是 -> 只需将评论添加到现有的帖子对象。

2/ 遍历您的字典以将其中包含的每个帖子添加到一个数组中。

3/ 按帖子的 id 对数组进行排序(这里我假设 id 与帖子的顺序相同,但只是一点建议:您可能应该有一个发布日期并按发布日期排序)。

如果您掌握 PHP 语法,您应该能够轻松实现它。如果您知道 PHP 语法,请不要犹豫提交编辑。

我希望这有帮助。

于 2012-09-17T01:32:05.860 回答
0

这段代码

    foreach($query->result() as $row){
      // if the post hasn't been created in the array
      // create it
        if(!isset($post[$row->post_id])){
            $post[$row->post_id] = array(
              "post_id" => $row->post_id,
              "post_text" => $row->post_text
            );
        }
        // if a comment is found
        if(isset($row->comment_id)){
            $post[$row->post_id]['comments'][] = array(
              "comment_id" => $row->comment_id,
              "comment_text" => $row->comment_text
            );
        }
    }

    return $posts[] = array_values($post);

返回这个(我需要的):

   [
     {
       "post_id"   : "something",
       "post_text" : "something else"
     },
     {
       "post_id"   : "something",
       "post_text" : "something else",
       "comments" : [
         {"comment_id":1,"comment_text":"something"},
         {"comment_id":2,"comment_text":"something"}
       ]
     },
    ....
   ]
于 2012-09-23T23:26:32.383 回答