【问题标题】:How to put each rows in json index?如何将每一行放在json索引中?
【发布时间】:2016-04-05 09:00:05
【问题描述】:

抱歉标题误导了,反正我有这行代码:

$results = $this->db->select("SELECT * FROM user");
if(count($results) > 0)
{
     return '{"user": ' . json_encode($results) . '}';
}
return http_response_code(204);

上面的代码让我得到一个用户列表,实际上我得到了这个结果:

{
  "user": [
    {
      "id": "R89d",
      "description": "admin",
      "created": "2016-03-20 20:45:09",
      "lastUpdate": "2016-03-20 20:45:09"
    },
    {
      "id": "RB01",
      "description": "normal",
      "created": null,
      "lastUpdate": "2016-03-22 10:54:48"
    },
    {
      "id": "RT40",
      "description": "tester",
      "created": null,
      "lastUpdate": null
    }
  ]
}

但我需要得到这个结果:

{
"user": [
    {
      "details":{
      "id": "R89d",
      "description": "admin",
      "created": "2016-03-20 20:45:09",
      "lastUpdate": "2016-03-20 20:45:09"
      }
    },
    {
      "details":{
      "id": "RB01",
      "description": "normal",
      "created": null,
      "lastUpdate": "2016-03-22 10:54:48"
      }
    },
    {
     "details":{
      "id": "RT40",
      "description": "tester",
      "created": null,
      "lastUpdate": null
      }
    }
  ]
}

如何查看每个用户在索引details 内。我怎样才能快速轻松地做到这一点?

【问题讨论】:

    标签: php json api rest


    【解决方案1】:

    您可以使用array_map 将数据包装在详细信息对象中。像这样:

    <?php
    
    $results = $this->db->select("SELECT * FROM user");
    if(count($results) > 0)
    {
        $results = array_map(function($u) { return ['details' => $u]; }, $results);
        return '{"user": ' . json_encode($results) . '}';
    }
    return http_response_code(204);
    

    【讨论】:

      【解决方案2】:

      这就像说:

      $makes = array( 'car' => 'ford', 'car' => 'citron', 'car' => $anotherCar );
      

      您无法获取$makes['car'] 的值。

      但是,您可以存储一个临时数组以使用并将详细信息作为数组附加,如下所示:

      $temp = array();
      if(count($results) > 0)
      {
           foreach ($results as $v)
           {
               array_push($temp, $v);
           }
           return '{"user": ' . json_encode($temp) . '}';
      }
      

      但是details 将被替换为0,1,2 [...]

      【讨论】:

        【解决方案3】:

        一个简单的方法可能是使用 foreach...

        $results = $this->db->select("SELECT * FROM user");
        if(count($results) > 0)
        {
           $resultnew = array();
           foreach($results as $result)
           {
             $resultnew['details'][] = $result;
           }
            return '{"user": ' . json_encode($resultnew) . '}';
        }
        return http_response_code(204);
        

        【讨论】:

          【解决方案4】:

          请检查以下脚本。

              $results = $this->db->select("SELECT * FROM user");
              $a = json_decode($results,true);
          
              foreach ($a as $key => $value) {
                   $b[]['details'] = $value;
              }
          
          
              $new_result = json_encode($b);
          
             if(count($results) > 0)
             {
                return '{"user": ' . $new_result . '}';
              }
           return http_response_code(204);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-09-09
            • 1970-01-01
            • 1970-01-01
            • 2021-10-02
            • 1970-01-01
            • 1970-01-01
            • 2019-10-23
            相关资源
            最近更新 更多