user8
user8

Reputation: 3

How to make multi-dimension array from mysql return rows with codeigniter

how to make multi dimension array from sql return rows in codeigniter. return values from model holding all values in $res ?

  $res = $this->user_model->get_room_book_join(['rooms.hotel_id' => 1]);
  if ($res) {
  echo '<pre>'
  print_r($res)
  }

And this is my model code.

  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();
   }

I getting this type of array.this type can not helping me.

   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
    )
 )

Actually I want this type of array. How can i make it. The multidimensional array will be created when ['room_id'] and ['rom_id'] both are equal then ['dates'] will be holding all date in single array [dates].

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
)))

Upvotes: 0

Views: 42

Answers (2)

Touheed Khan
Touheed Khan

Reputation: 2151

Updated Answer

@mi6crazyheart good answer. I want to suggest little change. You can use this code to do the task. I don't think it does matter if I replace indexed key with 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>';

Old Answer

You can use this code to solve your problem.

I've changed the result array little bit, but I think result in this format will be more helpful.(You can get room id from key).

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

In any more help needed I'm happy to help.

Happy Coding!

Upvotes: 1

Suresh
Suresh

Reputation: 5987

Check out this sample example which uses normal PHP loops. Not much efficient but it'll do your job.

$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']][]=$value['dates'];
}

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

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

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

Upvotes: 1

Related Questions