【问题标题】:json encode using codeigniter framework使用 codeigniter 框架进行 json 编码
【发布时间】:2016-08-12 22:15:57
【问题描述】:

我在这里做一个 json_encode

  public function get_posts_for_category($user_id,$category_id,$page)
{
    $cat_id = $this->ApiModel->get_category_id($category_id);
    $total_row = $this->ApiModel->get_category_posts_count($cat_id->category);
    $per_page = 2;
    $total_pages = $total_row / $per_page;
    $posts = $this->ApiModel->get_category_posts($cat_id->category,$per_page,$page);
    $data = array();
    foreach($posts as $post)
    {
        $fav = $this->ApiModel->get_favourite($user_id,$post->pid);
        if($fav == 1)
        {
            $status = 'true';
        } 
        else 
        {
            $status = 'false'; 
        }
        $array = array('pid' => $post->pid, 'uid' => $post->uid, 'title' => $post->title, 'slug' => $post->slug, 'content' => $post->content, 'image' => $post->image, 'time_stamp' => $post->time_stamp);
        $data[] = array('page' => $page, 'posts' => $array, 'is_favorite' => $status);
    }
    echo strip_tags(json_encode($data));
}

我从上面的代码得到的输出是

但我想要这样的东西

【问题讨论】:

    标签: php json codeigniter


    【解决方案1】:
        <?php
        public function get_posts_for_category($user_id,$category_id,$page)
        {
        $cat_id = $this->ApiModel->get_category_id($category_id);
        $total_row = $this->ApiModel->get_category_posts_count($cat_id->category);
        $per_page = 2;
        $total_pages = $total_row / $per_page;
        $posts = $this->ApiModel->get_category_posts($cat_id->category,$per_page,$page);
        $data = array();
        foreach($posts as $post)
        {
        $fav = $this->ApiModel->get_favourite($user_id,$post->pid);
        if($fav == 1)
        {
        $status = 'true';
        } 
        else 
        {
        $status = 'false'; 
        }
        $array = array('pid' => $post->pid, 'uid' => $post->uid, 'title' => $post->title, 'slug' => $post->slug, 'content' => $post->content, 'image' => $post->image, 'time_stamp' => $post->time_stamp, 'is_favorite' => $status);
        $data[] = array('page' => $page, 'posts' => $array, 'total_posts' => count($posts), 'total_pages' => $total_pages);
        }
        echo strip_tags(json_encode($data));
        }
        ?>
    

    【讨论】:

      【解决方案2】:
          $return = array(
              'page' => $page,
              'total_posts' => $total_row,
              'total_page' => $total_pages,
          );
          $data = [];
          foreach($posts as $post)
          {
              $fav = $this->ApiModel->get_favourite($user_id, $post->pid);
              if($fav == 1)
              {
                  $status = 'true';
              }
              else
              {
                  $status = 'false';
              }
              $array = array('pid' => $post->pid, 'uid' => $post->uid, 'title' => $post->title, 'slug' => $post->slug, 'content' => $post->content, 'image' => $post->image, 'time_stamp' => $post->time_stamp, 'is_favorite' => $status);
              $data[] = $array;
          }
          $return['posts'] = $data;
          $this->output->set_content_type('application/json');
          $this->output->set_output(json_encode($return));
      

      只需将此代码替换为您的 foreachecho 语句即可。

      您永远不应该将strip_tagsjson_encode 一起使用,因为您已经在对数据进行编码,接收者可以选择做他想做的任何事情,在此过程中您可能会破坏编码。

      您还应该使用CI 输出库来发送响应,您不得从控制器回显任何内容。更多详情请看这里https://ellislab.com/codeigniter/user-guide/libraries/output.html

      【讨论】:

        【解决方案3】:

        如果您希望这样输出数值(而不是字符串),您不妨尝试一下:

        json_encode($return, JSON_NUMERIC_CHECK);
        

        要很好地格式化输出的 JSON(缩进等),请使用:

        json_encode($return, JSON_PRETTY_PRINT);
        

        您可以按照json_encode's options 使用上述各种组合。

        至于布尔值(而不是作为字符串返回的“true”和“false”),您需要先将它们转换为布尔类型:

        $status = (bool) true;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-04-08
          • 1970-01-01
          • 2010-11-15
          • 1970-01-01
          • 2013-01-15
          • 2012-06-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多