如何将对象插入到 codeigniter 中的列表中
how to insert an object into the list in codeigniter
来自
{
"status": 1,
"message": "Success",
"data": [
{
"order": {
"id_biling": "1",
"order_id": "A18",
"kd_bill": "001001202101200001",
"order_name": "Kita",
"amount": "20000"
},
"detail": [
{
"kd_bill": "001001202101200001",
"name": "Odading",
"id_item": "66",
"amount": "10000",
"subtot": "20000",
"jml": "2"
}
]
}
]
}
至此
{
"status": 1,
"message": "Success",
"data": [
{
"order": {
"id_biling": "1",
"order_id": "A18",
"kd_bill": "001001202101200001",
"order_name": "Kita",
"amount": "20000",
"detail": [
{
"kd_bill": "001001202101200001",
"name": "Odading",
"id_item": "66",
"amount": "10000",
"subtot": "20000",
"jml": "2"
}
]
}
}
]
}
我的代码
$output = [];
foreach($listorder->result() as $id){
$data['order'] = $this->db->query("SELECT a.id_biling
,a.order_id
,a.kd_bill,a.order_name
,a.amount
from pos_biling_kasir
a left join pos_ref_tenant b on a.id_tenant
=b.id
where a.kd_bill='".$id->kd_bill."'")->row();
$data['detail'] = $this->db->query('SELECT *,sum(a.amount) as subtot,count(a.id_item) as jml from (select a.kd_bill
,b.name
,a.id_item,a.amount from pos_transaction_tenant
a left join pos_ref_item
b on a.id_item
=b.id
where a.kd_bill
='.$id->kd_bill.' ) 按 a.name 分组')->结果();
array_push($output, $data);
}
$jml = $listorder->num_rows();
if($jml > 0){
$response = [
'status' => 1,
'message' => 'Success',
'data' => $output
];
echo json_encode($response);
}else{
$response = [
'status' => 0,
'message' => 'Failed',
'data' => new stdClass()
];
echo json_encode($response);
}
您可以通过这种方式删除 detail 并将其作为元素推送到 order 中,如下所示-,
$decoded_response = json_decode($response, true);
foreach($decoded_response['data'][0] as $key=>$value){
if ($key == 'detail'){
$decoded_response['data'][0]['order'][$key] = $value;
unset($decoded_response['data'][0][$key]);
}
}
echo json_encode($decoded_response);
工作演示: https://3v4l.org/u67kE
来自
{
"status": 1,
"message": "Success",
"data": [
{
"order": {
"id_biling": "1",
"order_id": "A18",
"kd_bill": "001001202101200001",
"order_name": "Kita",
"amount": "20000"
},
"detail": [
{
"kd_bill": "001001202101200001",
"name": "Odading",
"id_item": "66",
"amount": "10000",
"subtot": "20000",
"jml": "2"
}
]
}
]
}
至此
{
"status": 1,
"message": "Success",
"data": [
{
"order": {
"id_biling": "1",
"order_id": "A18",
"kd_bill": "001001202101200001",
"order_name": "Kita",
"amount": "20000",
"detail": [
{
"kd_bill": "001001202101200001",
"name": "Odading",
"id_item": "66",
"amount": "10000",
"subtot": "20000",
"jml": "2"
}
]
}
}
]
}
我的代码
$output = [];
foreach($listorder->result() as $id){
$data['order'] = $this->db->query("SELECT a.id_biling
,a.order_id
,a.kd_bill,a.order_name
,a.amount
from pos_biling_kasir
a left join pos_ref_tenant b on a.id_tenant
=b.id
where a.kd_bill='".$id->kd_bill."'")->row();
$data['detail'] = $this->db->query('SELECT *,sum(a.amount) as subtot,count(a.id_item) as jml from (select a.kd_bill
,b.name
,a.id_item,a.amount from pos_transaction_tenant
a left join pos_ref_item
b on a.id_item
=b.id
where a.kd_bill
='.$id->kd_bill.' ) 按 a.name 分组')->结果();
array_push($output, $data);
}
$jml = $listorder->num_rows();
if($jml > 0){
$response = [
'status' => 1,
'message' => 'Success',
'data' => $output
];
echo json_encode($response);
}else{
$response = [
'status' => 0,
'message' => 'Failed',
'data' => new stdClass()
];
echo json_encode($response);
}
您可以通过这种方式删除 detail 并将其作为元素推送到 order 中,如下所示-,
$decoded_response = json_decode($response, true);
foreach($decoded_response['data'][0] as $key=>$value){
if ($key == 'detail'){
$decoded_response['data'][0]['order'][$key] = $value;
unset($decoded_response['data'][0][$key]);
}
}
echo json_encode($decoded_response);
工作演示: https://3v4l.org/u67kE