在某些逻辑下加入多个数组

时间:2016-07-27 08:56:25

标签: php arrays codeigniter

我有不同的数组如下: -

$reservations = ['reservationid' => '1', 'reservationid' => '2', 'reservationid' => '3'] 

$reservedrooms = ['reservationid'=> '1', 'roomno' => '1', 'reservationid' => '1', 'roomid'=>'2', 'reservationid' => '2', 'roomid'=>'3']

$guestdata = ['reservationid'=> '1', 'guestname' => 'Adam', 'reservationid' => '1', 'guestname'=>'Abraham', 'reservationid' => '2', 'guestname'=>'David']

如何连接它们以便它们可以用作CodeIgniter中具有类似预留条件的嵌套数组?我还使用Json将其发送到客户端,并在JQuery Datatables中进行描述。如何在逻辑上实现它?

我有以下代码在多个表上运行

$this->db->from('hotelbrancheshasreservations');
$this->db->join('reservation', 'reservation.reservationid = hotelbrancheshasreservations.reservations_reservationsid');
$this->db->join('reservedrooms', 'reservedrooms.reservation_reservationid = hotelbrancheshasreservations.reservations_reservationsid');
$this->db->where( array('hotelbranches_hotelbranchesid'=>$branchid, 'status'=>$status));
$reservations = $this->db->get()->result_array();

我想要结果数组如下: -

$result = array (
"reservationid" => '1', 'rooms' => Array(
    "roomno" => '1',
    "roomno" => '2'), 'guests' => Array('guestid' => '1', 'guestname' => 'Adam', 'guestid' => '2', 'guestname' => 'David',) ); 

1 个答案:

答案 0 :(得分:0)

以下代码在模型类中运行,然后返回到控制器。然后控制器echos json对象数组到客户端。

$this->$db->trans_start();


$this->$db->from('hotelbrancheshasreservations');
$this->$db->join('reservation', 'reservation.reservationid = hotelbrancheshasreservations.reservations_reservationsid');
$this->$db->join('reservedrooms', 'reservedrooms.reservation_reservationid = hotelbrancheshasreservations.reservations_reservationsid');
$this->$db->where( array('hotelbranches_hotelbranchesid'=>$branchid, 'status'=>$status));
$reservations = $this->$db->get()->result_array();
$this->$db->trans_complete();
return $reservations;

以上代码在加入后返回以下数组,并且在此解决方案问题中,如果预订有多个预留房间,那么该行多次包含在结果集中,这就是为什么我需要在预订中嵌套预留房间阵列。

以下是结果集:

{"data":[{"hotelbrancheshasreservationsid":"18","hotelbranches_hotelbranchesid":"1","reservations_reservationsid":"KHAN2016Q221","status":"active","tblid":"19","reservationid":"KHAN2016Q221","startdate":"2016-05-16 11:59:00","enddate":"2016-05-21 11:59:00","duration":"","noofroom":"","reservationdate":"2016-05-22 01:41:32","guestarrivaldate":"0000-00-00 00:00:00","gestdeparturedate":"0000-00-00 00:00:00","totalreservedadults":"","totalreservedchilds":"","totalreservationcharges":"","createdby":"","createdon":"0000-00-00 00:00:00","lastmodifiedby":"","lastmodifiedon":"0000-00-00 00:00:00","reservedroomsid":"1","reservation_reservationid":"KHAN2016Q221","hotelrooms_hotelroomsid":"1"},{"hotelbrancheshasreservationsid":"145","hotelbranches_hotelbranchesid":"1","reservations_reservationsid":"KHAN2016Q3-34","status":"active","tblid":"146","reservationid":"KHAN2016Q3-34","startdate":"2016-07-24 00:22:31","enddate":"2016-07-25 00:22:37","duration":"1 days 6 seconds ","noofroom":"2","reservationdate":"2016-07-24 00:26:51","guestarrivaldate":"2016-07-24 00:26:30","gestdeparturedate":"2016-07-25 00:26:35","totalreservedadults":"2","totalreservedchilds":"2","totalreservationcharges":"1200","createdby":"1","createdon":"2016-07-24 00:23:05","lastmodifiedby":"","lastmodifiedon":"0000-00-00 00:00:00","reservedroomsid":"91","reservation_reservationid":"KHAN2016Q3-34","hotelrooms_hotelroomsid":"2"},{"hotelbrancheshasreservationsid":"145","hotelbranches_hotelbranchesid":"1","reservations_reservationsid":"KHAN2016Q3-34","status":"active","tblid":"146","reservationid":"KHAN2016Q3-34","startdate":"2016-07-24 00:22:31","enddate":"2016-07-25 00:22:37","duration":"1 days 6 seconds ","noofroom":"2","reservationdate":"2016-07-24 00:26:51","guestarrivaldate":"2016-07-24 00:26:30","gestdeparturedate":"2016-07-25 00:26:35","totalreservedadults":"2","totalreservedchilds":"2","totalreservationcharges":"1200","createdby":"1","createdon":"2016-07-24 00:23:15","lastmodifiedby":"","lastmodifiedon":"0000-00-00 00:00:00","reservedroomsid":"92","reservation_reservationid":"KHAN2016Q3-34","hotelrooms_hotelroomsid":"1"},{"hotelbrancheshasreservationsid":"146","hotelbranches_hotelbranchesid":"1","reservations_reservationsid":"KHAN2016Q3-35","status":"active","tblid":"147","reservationid":"KHAN2016Q3-35","startdate":"2016-07-25 22:05:36","enddate":"2016-07-26 22:05:41","duration":"1 days 5 seconds ","noofroom":"1","reservationdate":"2016-07-25 22:08:07","guestarrivaldate":"2016-07-25 22:07:53","gestdeparturedate":"2016-07-26 22:07:57","totalreservedadults":"1","totalreservedchilds":"","totalreservationcharges":"1500","createdby":"1","createdon":"2016-07-25 22:05:52","lastmodifiedby":"","lastmodifiedon":"0000-00-00 00:00:00","reservedroomsid":"93","reservation_reservationid":"KHAN2016Q3-35","hotelrooms_hotelroomsid":"4"},{"hotelbrancheshasreservationsid":"147","hotelbranches_hotelbranchesid":"1","reservations_reservationsid":"KHAN2016Q3-36","status":"active","tblid":"148","reservationid":"KHAN2016Q3-36","startdate":"2016-07-26 10:54:05","enddate":"2016-07-27 10:54:10","duration":"1 days 5 seconds ","noofroom":"1","reservationdate":"2016-07-26 10:56:15","guestarrivaldate":"2016-07-26 10:55:58","gestdeparturedate":"2016-07-27 10:56:03","totalreservedadults":"1","totalreservedchilds":"","totalreservationcharges":"700","createdby":"1","createdon":"2016-07-26 10:54:28","lastmodifiedby":"","lastmodifiedon":"0000-00-00 00:00:00","reservedroomsid":"94","reservation_reservationid":"KHAN2016Q3-36","hotelrooms_hotelroomsid":"3"}]}