【发布时间】:2016-01-17 09:08:46
【问题描述】:
使用我目前的代码,我可以使用 json_encode 从我的数据库中获取正确的详细信息。
[{
"tour_id": "1",
"tour_name": "Test this",
"start": "2016-01-12 13:21:32",
"end": "2016-01-15 13:21:35",
"itinerary_photo": "Desert.jpg"
}][{
"tour_id": "3",
"tour_name": "Everest",
"start": "2016-02-10 13:23:00",
"end": "2016-02-18 13:23:07",
"itinerary_photo": "Chrysanthemum.jpg"
}]
预期的 JSON 馈送
我正在使用 json 提要向 fullcalendar 添加事件。而我的 json_encode 结果的格式应该是这样的:
[{
"tour_id": "1",
"tour_name": "Test this",
"start": "2016-01-12 13:21:32",
"end": "2016-01-15 13:21:35",
"itinerary_photo": "Desert.jpg"
}, {
"tour_id": "3",
"tour_name": "Everest",
"start": "2016-02-10 13:23:00",
"end": "2016-02-18 13:23:07",
"itinerary_photo": "Chrysanthemum.jpg"
}]
不同之处在于对象仅存储在一个数组中。随着我得到的输出,它显示了多个数组。
这是我的控制器代码:
public function getBookedCal(){
if ($this->session->userdata('login')==true){
$data['twimembers'] = $this->session->userdata('twimembers');
$data['tour'] = $this->Model_MyBookedTours->getPaidTours($data['twimembers']['user_id']);
foreach ($data['tour'] as $tourConfirm){
$tourConfirm = $this->Model_MyBookedTours->getTourInfo($tourConfirm['Tour_packages_tourpkg_id']);
echo json_encode($tourConfirm);
}
//echo json_encode($tourConfirm); //this outputs only a single array
}//end if
}//end function
型号:
function getPaidTours($user_id){
$this->db->where('User_accounts_user_id',$user_id);
$this->db->where('payment_status','1'); //confirmed
$query = $this->db->get('user_tourpkg');
return $query->result_array();
} //calendar
function getTourInfo($tour_id){
$this->db->select('tour_id,tour_name, start, end, itinerary_photo');
$this->db->where('tour_id',$tour_id);
$query = $this->db->get('tour_packages');
return $query->result();
}
有什么方法可以通过对我的代码进行一些调整来获得我预期的 json 提要?
【问题讨论】:
-
现在你的问题是什么?
-
你试过
$array3 = array_merge($array1,$array2);或$array1 + $array2 = $array3;吗? -
@devpro 有什么办法可以通过对我的代码进行一些调整来获得我预期的 json 提要?
-
@Dray 知道我从数据库中获取了我的 json 提要,我认为如果将额外的数据添加到数据库中,合并单个数组将不起作用。但我明白你的意思!
-
@Laurel 为什么在向数据库中添加其他数据时它不起作用?
标签: php json codeigniter model-view-controller fullcalendar