如何使用codeigniter从mysql返回行创建多维数组

时间:2017-04-18 09:47:43

标签: php codeigniter

如何从codeigniter中的sql返回行创建多维数组。从包含$ res?

中所有值的模型返回值
  $res = $this->user_model->get_room_book_join(['rooms.hotel_id' => 1]);
  if ($res) {
  echo '<pre>'
  print_r($res)
  }

这是我的型号代码。

  function get_room_book_join($data) {
    $this->db->join('room_overviews', 'rooms.room_id = 
    room_overviews.rom_id', 'left');
    $q = $this->db->get_where('rooms', $data);
    return $q->result_array();
   }

我得到了这种类型的数据。这种类型无法帮助我。

   Array(
   [0] => Array(
        [room_id] => 1
        [room_no] => 101
        [room_desc] =>  Double Bed Deluxe Non Air Conditioned
        [status] => available
        [category_id] => 1
        [hotel_id] => 1
        [tariff_type] => normal
        [room_rate] => 1000
        [persons] => 0
        [date_start] => 0000-00-00
        [date_end] => 0000-00-00
        [overview_id] => 1
        [rom_id] => 1
        [hot_id] => 1
        [cus_id] => 2
        [bok_id] => 2
        [dates] => 2017-04-12
    )
   [1] => Array
    (
        [room_id] => 1
        [room_no] => 101
        [room_desc] =>  Double Bed Deluxe Non Air Conditioned
        [status] => available
        [category_id] => 1
        [hotel_id] => 1
        [tariff_type] => normal
        [room_rate] => 1000
        [persons] => 0
        [date_start] => 0000-00-00
        [date_end] => 0000-00-00
        [overview_id] => 2
        [rom_id] => 1
        [hot_id] => 1
        [cus_id] => 2
        [bok_id] => 2
        [dates] => 2017-04-13
    )
   [2] => Array
    (
        [room_id] => 1
        [room_no] => 101
        [room_desc] =>  Double Bed Deluxe Non Air Conditioned
        [status] => available
        [category_id] => 1
        [hotel_id] => 1
        [tariff_type] => normal
        [room_rate] => 1000
        [persons] => 0
        [date_start] => 0000-00-00
        [date_end] => 0000-00-00
        [overview_id] => 3
        [rom_id] => 1
        [hot_id] => 1
        [cus_id] => 2
        [bok_id] => 2
        [dates] => 2017-04-14
    )
   [3] => Array
    (
        [room_id] => 1
        [room_no] => 101
        [room_desc] =>  Double Bed Deluxe Non Air Conditioned
        [status] => available
        [category_id] => 1
        [hotel_id] => 1
        [tariff_type] => normal
        [room_rate] => 1000
        [persons] => 0
        [date_start] => 0000-00-00
        [date_end] => 0000-00-00
        [overview_id] => 4
        [rom_id] => 1
        [hot_id] => 1
        [cus_id] => 2
        [bok_id] => 2
        [dates] => 2017-04-15
    )
   [4] => Array
    (
        [room_id] => 2
        [room_no] => 102
        [room_desc] =>  Double Bed Deluxe Non Air Conditioned Room
        [status] => available
        [category_id] => 1
        [hotel_id] => 1
        [tariff_type] => normal
        [room_rate] => 1000
        [persons] => 2
        [date_start] => 0000-00-00
        [date_end] => 0000-00-00
        [overview_id] => 5
        [rom_id] => 2
        [hot_id] => 1
        [cus_id] => 2
        [bok_id] => 2
        [dates] => 2017-04-12
    )
   [5] => Array
    (
        [room_id] => 2
        [room_no] => 102
        [room_desc] =>  Double Bed Deluxe Non Air Conditioned Room
        [status] => available
        [category_id] => 1
        [hotel_id] => 1
        [tariff_type] => normal
        [room_rate] => 1000
        [persons] => 2
        [date_start] => 0000-00-00
        [date_end] => 0000-00-00
        [overview_id] => 6
        [rom_id] => 2
        [hot_id] => 1
        [cus_id] => 2
        [bok_id] => 2
        [dates] => 2017-04-13
    )
   [6] => Array
    (
        [room_id] => 2
        [room_no] => 102
        [room_desc] =>  Double Bed Deluxe Non Air Conditioned Room
        [status] => available
        [category_id] => 1
        [hotel_id] => 1
        [tariff_type] => normal
        [room_rate] => 1000
        [persons] => 2
        [date_start] => 0000-00-00
        [date_end] => 0000-00-00
        [overview_id] => 7
        [rom_id] => 2
        [hot_id] => 1
        [cus_id] => 2
        [bok_id] => 2
        [dates] => 2017-04-14
    )
   [7] => Array
    (
        [room_id] => 2
        [room_no] => 102
        [room_desc] =>  Double Bed Deluxe Non Air Conditioned Room
        [status] => available
        [category_id] => 1
        [hotel_id] => 1
        [tariff_type] => normal
        [room_rate] => 1000
        [persons] => 2
        [date_start] => 0000-00-00
        [date_end] => 0000-00-00
        [overview_id] => 8
        [rom_id] => 2
        [hot_id] => 1
        [cus_id] => 2
        [bok_id] => 2
        [dates] => 2017-04-15
    )
 )

其实我想要这种类型的数组。我怎么能做到当[&#39; room_id&#39;]和[&#39; rom_id&#39;]都相等时,将创建多维数组,然后[&#39; dates&#39;]将在单个数组中保存所有日期[日期]。

array(
 [0]=>array(
  [room_id]=>1
  [dates]=>array(
   [0]=>2014-04-12
   [1]=>2014-04-13
   [2]=>2014-04-14
   [3]=>2014-04-15
  )
 )
[1]=>array(
 [room_id]=>2
 [dates]=>array(
  [0]=>2014-04-12
  [1]=>2014-04-13
  [2]=>2014-04-14
  [3]=>2014-04-15
)))

2 个答案:

答案 0 :(得分:1)

查看此示例示例,该示例使用普通的PHP循环。没有多少效率,但它会完成你的工作。

data: function() {
   return { myList: [] }
},

watch: {
   myList: this.handleListChange
},

methods: {
   handleListChange: function(a, b) {
       // .. do whatever you want to do in the watch method
   }
}

答案 1 :(得分:1)

更新了答案

@ mi6crazyheart 很好的答案。我想建议一点变化。您可以使用此代码执行该任务。如果我用room_id替换索引密钥,我认为这并不重要。

$dbResponse[] = array(
        'room_id' => 1,
        'room_no' => 1,
        'dates' => '2017-04-12',
    );
$dbResponse[] = array(
        'room_id' => 1,
        'room_no' => 1,
        'dates' => '2017-04-13',
    );
$dbResponse[] = array(
        'room_id' => 2,
        'room_no' => 1,
        'dates' => '2017-04-14',
    );
$dbResponse[] = array(
        'room_id' => 2,
        'room_no' => 1,
        'dates' => '2017-04-15',
    );

$updatedList = array();
foreach ($dbResponse as $key => $value) {
    $updatedList[$value['room_id']]['room_id']=$value['room_id'];
    $updatedList[$value['room_id']]['dates'][]=$value['dates'];
}

echo '<pre>';
print_r($updatedList);
echo '</pre>';

旧答案

您可以使用此代码解决问题。

我已经稍微更改了结果数组,但我认为这种格式的结果会更有帮助。(你可以从键获得房间ID)。

$result = array()
foreach ($res as $row) {
    if($row['room_id'] === $row['rom_id'])
        $result[$res['room_id']]['dates'][] = $row['dates'];
}

如果需要更多帮助,我很乐意提供帮助。

快乐编码!