【问题标题】:Need help setting up json array需要帮助设置 json 数组
【发布时间】:2012-09-16 05:38:33
【问题描述】:

一个数据库查询返回几行,我循环如下:

    foreach ($query->result() as $row) {

        $data[$row->post_id]['post_id']         = $row->post_id;
        $data[$row->post_id]['post_type']       = $row->post_type;
        $data[$row->post_id]['post_text']       = $row->post_text;
    }

如果我 json_encode 得到结果数组 ($a['stream']) 我得到 ​​p>

{
    "stream": {
        "1029": {
            "post_id": "1029",
            "post_type": "1",
            "post_text": "bla1",
        },
        "1029": {
            "post_id": "1030",
            "post_type": "3",
            "post_text": "bla2",
        },
        "1029": {
            "post_id": "1031",
            "post_type": "2",
            "post_text": "bla3",            
        }
    }
}

json 实际上应该是这样的:

{
    "stream": {
        "posts": [{
            "post_id": "1029",
            "post_type": "1",
            "post_text": "bla1",
        },
        {
            "post_id": "1030",
            "post_type": "3",
            "post_text": "bla2",
        },
        {
            "post_id": "1031",
            "post_type": "2",
            "post_text": "bla3",            
        }]
    }
}

我应该如何构建我的数组来获得这个json 对吗?

【问题讨论】:

    标签: php javascript arrays json jsonp


    【解决方案1】:

    无论如何,这是你应该做的:

    ...
    $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;
    ...
    

    一点解释:您必须根据从数据库中获得的信息构建一个对象,即$post。这些对象中的每一个都需要一起添加到一个数组中,即$posts。来自数据库的这组帖子被设置为$data 的键posts,即$data['posts']

    【讨论】:

      【解决方案2】:

      这个怎么样?

      http://codepad.viper-7.com/zPHCm0

      <?php
      $myData = array();
      $myData['posts'][] = array('post_id' => 3, 'post_type' => 343, 'post_text' => 'sky muffin pie');
      $myData['posts'][] = array('post_id' => 4, 'post_type' => 111, 'post_text' => 'Mushroom chocolate banana');
      $myData['posts'][] = array('post_id' => 231, 'post_type' => 888, 'post_text' => 'Cucumber strawberry in the sky');
      
      $theStream['stream'] = $myData;
      
      $json = json_encode($theStream);
      
      echo 'JSON:<Br/>';
      echo $json;
      

      上面给了我:

      {
          "stream":
          {   "posts":[
                  {"post_id":3,"post_type":343,"post_text":"sky muffin pie"},
                  {"post_id":4,"post_type":111,"post_text":"Mushroom chocolate banana"},
                  {"post_id":231,"post_type":888,"post_text":"Cucumber strawberry in the sky"}]
          }
      } 
      

      【讨论】:

        【解决方案3】:
        foreach ($query->result() as $row) {
            $data['posts']['post_id']         = $row->post_id;
            $data['posts']['post_type']       = $row->post_type;
            $data['posts']['post_text']       = $row->post_text;
        }
        

        【讨论】:

          猜你喜欢
          • 2021-12-13
          • 2010-12-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-02-14
          • 1970-01-01
          • 2015-07-16
          相关资源
          最近更新 更多