【问题标题】:format rest Api group field by category按类别格式化 rest Api 组字段
【发布时间】:2020-06-09 09:54:35
【问题描述】:

我是 API 新手,我使用 Codeigniter 从 MySql 数据库产品创建 API 字段:金额、数量、客户姓名、客户电话、客户地址 结果看起来:

```[
    {
        "Id": "1",
        "Amount": "21542",
        "quantity": "52",
        "customerName": "John",
        "CustomerPhone": "254215",
        "CustomerAddress": "road tvz120",

    },```

但我希望它看起来像这样:

```[
    {
        "Id": "1",
        "Amount": "21542",
        "quantity": "52",
        "customerInfo":{
        "customerName": "John",
        "CustomerPhone": "254215",
        "CustomerAddress": "rue tvz120"},

    },```

我的意思是将与客户相关的 3 个字段与名称客户信息进行分组

我的php代码是

```public function index_get($id = 0)
    {
        if(!empty($id)){
            $data = $this->db->get_where("Products", ['Id' => $id])->row_array();
        }else{
            $data = $this->db->get("Products")->result();
        }

        $this->response($data, REST_Controller::HTTP_OK);
    }```

【问题讨论】:

    标签: php mysql api rest codeigniter


    【解决方案1】:

    您的产品格式是基于数据库的,因此如果您想更改结果格式,您必须手动构建它。您需要在返回数据之前循环结果。

    if(!empty($id)){
        $data = $this->db->get_where("Products", ['Id' => $id])->row_array();
    }else{
        $data = $this->db->get("Products")->result();
        $newdata = array();
    
        foreach($data as $row)
        {
            $newdata[] = array(
                "Id" => $row->id,
                "Amount" => $row->amount,
                "quantity" => $row->quantity,
                "customerInfo" => array(
                    "customerName" => $row->customerName,
                    "CustomerPhone" => $row->CustomerPhone,
                    "CustomerAddress" => $row->CustomerAddress,
                )
            );
        }
        $data = $newdata;
    }
    $this->response($data, REST_Controller::HTTP_OK);
    

    【讨论】:

      【解决方案2】:

      您不能通过查询执行此操作,您必须遍历结果并将其更改为所需的格式,就像这样 -

      if(!empty($id)){
      
          $result = $this->db->get_where("Products", ['Id' => $id])->result(); 
          // I'll advise to use result() here instead of row_array() so that you don't face any issue when there's only one row.
      
      }else{
      
          $result = $this->db->get("Products")->result();
      
      }
      
      foreach($result as $res){
      
          $new_result[] = array(
              "Id"           => $res->Id,
              "Amount"       => $res->Amount,
              "quantity"     => $res->quantity,
              "customerInfo" => array(
                                      "customerName"    => $res->customerName,
                                      "CustomerPhone"   => $res->CustomerPhone,
                                      "CustomerAddress" => $res->CustomerAddress
                                      )
                          );
      }
      $this->response($new_result, REST_Controller::HTTP_OK); // send newly created array instead
      

      看看这对你有没有帮助。

      【讨论】:

        猜你喜欢
        • 2017-02-21
        • 2021-03-07
        • 2017-07-26
        • 2019-10-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-12
        相关资源
        最近更新 更多