2 回答

TA貢獻1744條經(jīng)驗 獲得超4個贊
你的 Products 格式是基于數(shù)據(jù)庫的,因此如果你想改變結(jié)果格式,你必須手動構(gòu)建它。您需要在返回數(shù)據(jù)之前循環(huán)結(jié)果。
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);

TA貢獻1827條經(jīng)驗 獲得超4個贊
您不能通過查詢執(zhí)行此操作,您必須遍歷結(jié)果并將其更改為所需的格式,就像這樣 -
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
看看這是否對您有幫助。
- 2 回答
- 0 關(guān)注
- 159 瀏覽
添加回答
舉報