php - Combine sub-arrays of an array that have the same key -


i'm trying populate array of array room_id should combined in 1 sub-array instead of separated are. here code:

$query = "select res_id, room_id, guest_id, check_in_date, check_out_date reservation ";  $result = mysqli_query($link, $query) or die (mysqli_error($link)); $rooms = array(); while ($row = mysqli_fetch_assoc($result)) {     $rooms[] = $row; }  $array_date = array();  foreach ($rooms $room) {     $array_date[] = date_range($room['room_id'], $room['check_in_date'], $room['check_out_date']); }  function date_range($room_id, $first, $last, $step = '+1 day', $output_format = 'y-m-d' ) {      $room_date = array();     $dates = array();     $current = strtotime($first);     $last = strtotime($last);      while( $current <= $last ) {         $dates[] = date($output_format, $current);         $current = strtotime($step, $current);     }     $room_date[$room_id] = $dates;      return $room_date; } 

here result var_dump:

array (     [0] => array         (             [1] => array                 (                     [0] => 2015-04-08                     [1] => 2015-04-09                     [2] => 2015-04-10                 )          )      [1] => array         (             [1] => array                 (                     [0] => 2015-04-11                     [1] => 2015-04-12                     [2] => 2015-04-13                 )          )      [2] => array         (             [2] => array                 (                     [0] => 2015-04-08                     [1] => 2015-04-09                     [2] => 2015-04-10                     [3] => 2015-04-11                     [4] => 2015-04-12                     [5] => 2015-04-13                 )          ) 

what want (same room_id combined in 1 array):

array (     [0] => array         (             [1] => array                 (                     [0] => 2015-04-08                     [1] => 2015-04-09                     [2] => 2015-04-10                     [3] => 2015-04-11                     [4] => 2015-04-12                     [5] => 2015-04-13                 )          )     [2] => array         (             [2] => array                 (                     [0] => 2015-04-08                     [1] => 2015-04-09                     [2] => 2015-04-10                     [3] => 2015-04-11                     [4] => 2015-04-12                     [5] => 2015-04-13                 )         ) ) 

i ran out of ideas. how can combine arrays have same room_id?

i don't think second example structure you'd like--there's no need have zero-indexed first dimension if you're trying store dates each room number, dimension gets in way. think best way pass existing subarray reference function can directly add elements onto array. rewrote code (and removed redundancy/inefficiency), loop, produce think want:

$query = "select res_id, room_id, guest_id, check_in_date, check_out_date reservation ";  $result = mysqli_query($link, $query) or die (mysqli_error($link)); $rooms = array(); $array_date = array();  while ($row = mysqli_fetch_assoc($result)) {     $rooms[] = $row; // no longer used, i'm assuming want else later      if (!array_key_exists($row['room_id'], $array_date)) {         $array_date[$row['room_id']] = array();         date_range($array_date[$row['room_id']], $row['room_id'], $row['check_in_date'], $row['check_out_date']);     } else {         date_range($array_date[$row['room_id']], $row['room_id'], $row['check_in_date'], $row['check_out_date']);     } }    function date_range(&$currentdates, $room_id, $first, $last, $step = '+1 day', $output_format = 'y-m-d' ) {      $current = strtotime($first);     $last = strtotime($last);      while( $current <= $last ) {         $currentdates[] = date($output_format, $current);         $current = strtotime($step, $current);     } } 

this should produce array this:

array (     [1] => array // room id     (         [0] => 2015-04-08         [1] => 2015-04-09         [2] => 2015-04-10         [3] => 2015-04-11         [4] => 2015-04-12         [5] => 2015-04-13     )     [2] => array // room id     (         [0] => 2015-04-08         [1] => 2015-04-09         [2] => 2015-04-10         [3] => 2015-04-11         [4] => 2015-04-12         [5] => 2015-04-13     ) ) 

so can iterate through dates available each room doing:

foreach ($array_date $roomnumber => $dates) {     foreach ($dates $date) {         //     } } 

this solution might allow duplicate entries dates, depending on how table set up. avoid duplicates, check skip adding date if room reserved replacing contents of while loop in date_range()

if (!in_array(date($output_format, $current), $currentdates)) {      $currentdates[] = date($output_format, $current); }  $current = strtotime($step, $current); 

hopefully behavior want. let me know if not!


Popular posts from this blog