如何将数组推入 json key/value 对象对 i codeigniter 数组
How to push an array into json key/value pair of an object i codeigniter array
我想达到什么目的
所以我想要实现的是将一个数组 key/value 对推入一个 JSON 对象,该对象本身就是一个数组,因此只有一个对象和一个键-值对包含一个数组。
这就是我想要实现的 作为 JSON 输出,另一个数据数组中的另一个数据数组。
[
{
"data": {
"temp_service_id": "3",
"name": "health checkup",
"price": "10000",
"service_id": "41",
"data2": [
{
"fees": "2000",
"service_name": "Anaesthesiologist"
},
{
"fees": "300",
"service_name": "Andrologist"
},
]
},
}
]
我试过的
这是我的控制器:
$where['subtype_id'] = $this->post('org_id');
$where['is_active'] = 'Y';
$table_package_master = 'package_master';
$fetch_package_name = $this->$model_name->fetch($table_package_master,$where);
$array = [];
$array1 = [];
$array2 = [];
if($fetch_package_name){
foreach($fetch_package_name as $row){
$where_r['t1.package_num'] = $row->package_num;
$where_r['t1.is_active'] = 'Y';
$where_r['services.is_active'] = 'Y';
$where_r['t4.is_active'] = 'Y';
$fetch_packages1 = $this->$model_name->fetch_packages1($where_r);
$array['data'] = $fetch_packages1;
$fetch_packages = $this->$model_name->fetch_packages($where_r);
foreach($fetch_packages as $row1){
$where_re['services.service_id'] = $row1->service_id;
$where_re['services.is_active'] = 'Y';
$where_re['template_services.is_active'] = 'Y';
$fetch_package_ser = $this->$model_name->fetch_service_details($where_re);
array_push($array1,$fetch_package_ser);
}
}
$array['data2'] = $array1;
$array3 = [$array];
$this->response($array3);
}
这是我的模特:
function fetch($table,$where_con){
$this->db->select('*')->from($table)->where($where_con);
return $this->db->get()->result();
}
function fetch_packages1($where){
$this->db->select('t3.temp_service_id,t4.name,t4.price,services.service_id');
$this->db->from('package_services as t1');
$this->db->join('services','services.service_id = t1.service_id', 'LEFT');
$this->db->join('template_services as t3','t3.temp_service_id = services.temp_service_id' , 'LEFT');
$this->db->join('package_master as t4', 't4.package_num = t1.package_num','LEFT');
$this->db->where($where);
$this->db->group_by('t1.package_num');
return $this->db->get()->row();
}
function fetch_service_details($where){
$this->db->select('services.price as fees,template_services.service_name');
$this->db->from('services');
$this->db->join('template_services','template_services.temp_service_id =
services.temp_service_id','LEFT');
$this->db->where($where);
return $this->db->get()->row();
}
基本上我想做的是将数据对象放入 $array 变量中,该变量是来自一个 table 的数据,现在我所做的是将数据从另一个查询推送到 $array1
最后我所做的只是 array_push 将来自 $array1 和 $array2 的所有数据 array_push 放入 $array3 中,使它们连接在一起,但是
我的输出是这样的:
[
{
"data": {
"temp_service_id": "3",
"name": "health checkup",
"price": "10000",
"service_id": "41"
},
"data2": [
{
"fees": "2000",
"service_name": "Anaesthesiologist"
},
{
"fees": "300",
"service_name": "Andrologist"
}
]
}
]
我在这里错过了什么?我真的很困惑,无法理解如何推送 data2 数组。
提前谢谢你:)
用 $array->data['data2'] = $array1;
在 Controller $array['data2'] = $array1;
中重写一行。
它会将数组 data2
放入 data
索引。
工作Demo
想了很久终于想到了解决办法
$fetch_package_name = $this->$model_name->fetch($table_package_master,$where);
$array = [];
$array1 = [];
if($fetch_package_name){
for($i=0;$i<sizeof($fetch_package_name); $i++){
$where_r['t1.package_num'] = $fetch_package_name[$i]->package_num;
$where_r['t1.is_active'] = 'Y';
$where_r['services.is_active'] = 'Y';
$where_r['t4.is_active'] = 'Y';
$fetch_packages1 = $this->$model_name->fetch_packages1($where_r);
$array[$i]['name'] = $fetch_package_name[$i]->name;
$array[$i]['price'] = $fetch_package_name[$i]->price;
$where_re['t1.package_num'] = $fetch_package_name[$i]->package_num;
$where_re['t1.is_active'] = 'Y';
$where_re['services.is_active'] = 'Y';
$fetch_packages = $this->$model_name->fetch_packages2($where_re);
for($j=0; $j<sizeof($fetch_packages); $j++){
$array1[$j]['service_name'] = $fetch_packages[$j]->service_name;
$array1[$j]['price'] = $fetch_packages[$j]->price;
}
$array[$i]['data2'] = $array1;
}
// $this->response($array);
$array2['data'] = $array;
$array2['status'] = 10007;
// $data_response['data'] = $array;
$this->response($array2);
}
它首先给了我想要的响应。\
{
"data":[
{
"name":"Full body checkup",
"price":"5000",
"data2":[
{
"service_name":"Allergist",
"price":"23"
},
{
"service_name":"Andrologist",
"price":"300"
}
]
}
],
}
干杯!快乐编码:-)
我想达到什么目的
所以我想要实现的是将一个数组 key/value 对推入一个 JSON 对象,该对象本身就是一个数组,因此只有一个对象和一个键-值对包含一个数组。
这就是我想要实现的 作为 JSON 输出,另一个数据数组中的另一个数据数组。
[
{
"data": {
"temp_service_id": "3",
"name": "health checkup",
"price": "10000",
"service_id": "41",
"data2": [
{
"fees": "2000",
"service_name": "Anaesthesiologist"
},
{
"fees": "300",
"service_name": "Andrologist"
},
]
},
}
]
我试过的
这是我的控制器:
$where['subtype_id'] = $this->post('org_id');
$where['is_active'] = 'Y';
$table_package_master = 'package_master';
$fetch_package_name = $this->$model_name->fetch($table_package_master,$where);
$array = [];
$array1 = [];
$array2 = [];
if($fetch_package_name){
foreach($fetch_package_name as $row){
$where_r['t1.package_num'] = $row->package_num;
$where_r['t1.is_active'] = 'Y';
$where_r['services.is_active'] = 'Y';
$where_r['t4.is_active'] = 'Y';
$fetch_packages1 = $this->$model_name->fetch_packages1($where_r);
$array['data'] = $fetch_packages1;
$fetch_packages = $this->$model_name->fetch_packages($where_r);
foreach($fetch_packages as $row1){
$where_re['services.service_id'] = $row1->service_id;
$where_re['services.is_active'] = 'Y';
$where_re['template_services.is_active'] = 'Y';
$fetch_package_ser = $this->$model_name->fetch_service_details($where_re);
array_push($array1,$fetch_package_ser);
}
}
$array['data2'] = $array1;
$array3 = [$array];
$this->response($array3);
}
这是我的模特:
function fetch($table,$where_con){
$this->db->select('*')->from($table)->where($where_con);
return $this->db->get()->result();
}
function fetch_packages1($where){
$this->db->select('t3.temp_service_id,t4.name,t4.price,services.service_id');
$this->db->from('package_services as t1');
$this->db->join('services','services.service_id = t1.service_id', 'LEFT');
$this->db->join('template_services as t3','t3.temp_service_id = services.temp_service_id' , 'LEFT');
$this->db->join('package_master as t4', 't4.package_num = t1.package_num','LEFT');
$this->db->where($where);
$this->db->group_by('t1.package_num');
return $this->db->get()->row();
}
function fetch_service_details($where){
$this->db->select('services.price as fees,template_services.service_name');
$this->db->from('services');
$this->db->join('template_services','template_services.temp_service_id =
services.temp_service_id','LEFT');
$this->db->where($where);
return $this->db->get()->row();
}
基本上我想做的是将数据对象放入 $array 变量中,该变量是来自一个 table 的数据,现在我所做的是将数据从另一个查询推送到 $array1 最后我所做的只是 array_push 将来自 $array1 和 $array2 的所有数据 array_push 放入 $array3 中,使它们连接在一起,但是 我的输出是这样的:
[
{
"data": {
"temp_service_id": "3",
"name": "health checkup",
"price": "10000",
"service_id": "41"
},
"data2": [
{
"fees": "2000",
"service_name": "Anaesthesiologist"
},
{
"fees": "300",
"service_name": "Andrologist"
}
]
}
]
我在这里错过了什么?我真的很困惑,无法理解如何推送 data2 数组。 提前谢谢你:)
用 $array->data['data2'] = $array1;
在 Controller $array['data2'] = $array1;
中重写一行。
它会将数组 data2
放入 data
索引。
工作Demo
想了很久终于想到了解决办法
$fetch_package_name = $this->$model_name->fetch($table_package_master,$where);
$array = [];
$array1 = [];
if($fetch_package_name){
for($i=0;$i<sizeof($fetch_package_name); $i++){
$where_r['t1.package_num'] = $fetch_package_name[$i]->package_num;
$where_r['t1.is_active'] = 'Y';
$where_r['services.is_active'] = 'Y';
$where_r['t4.is_active'] = 'Y';
$fetch_packages1 = $this->$model_name->fetch_packages1($where_r);
$array[$i]['name'] = $fetch_package_name[$i]->name;
$array[$i]['price'] = $fetch_package_name[$i]->price;
$where_re['t1.package_num'] = $fetch_package_name[$i]->package_num;
$where_re['t1.is_active'] = 'Y';
$where_re['services.is_active'] = 'Y';
$fetch_packages = $this->$model_name->fetch_packages2($where_re);
for($j=0; $j<sizeof($fetch_packages); $j++){
$array1[$j]['service_name'] = $fetch_packages[$j]->service_name;
$array1[$j]['price'] = $fetch_packages[$j]->price;
}
$array[$i]['data2'] = $array1;
}
// $this->response($array);
$array2['data'] = $array;
$array2['status'] = 10007;
// $data_response['data'] = $array;
$this->response($array2);
}
它首先给了我想要的响应。\
{
"data":[
{
"name":"Full body checkup",
"price":"5000",
"data2":[
{
"service_name":"Allergist",
"price":"23"
},
{
"service_name":"Andrologist",
"price":"300"
}
]
}
],
}
干杯!快乐编码:-)