组合具有相同键的数组的子数组

时间:2015-04-09 18:02:25

标签: php mysql multidimensional-array

我试图填充一个数组数组,其中room_id应该组合在一个子数组中,而不是像现在一样分开。这是我的代码:

$query = "SELECT res_id, room_id, guest_id, check_in_date, check_out_date FROM 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 as $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;
}

以下是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
                )

        )

我真正想要的是这个(同一个room_id合并在一个数组中):

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

我完全没有想法了。如何组合具有相同room_id的数组?

1 个答案:

答案 0 :(得分:1)

我不认为第二个例子实际上就是你喜欢的结构 - 如果你只是试图存储,那么就没有必要拥有零索引的第一维度每个房间号码的日期,以便维度只会妨碍。我认为最好的方法是通过引用函数传递现有的子数组,以便它可以直接将元素添加到数组中。我重写了你的代码(并且还删除了一些冗余/低效率),比如额外的循环,以产生我认为你想要的东西:

$query = "SELECT res_id, room_id, guest_id, check_in_date, check_out_date FROM reservation ";

$result = mysqli_query($link, $query) or die (mysqli_error($link));
$rooms = array();
$array_date = array();

while ($row = mysqli_fetch_assoc($result)) {
    $rooms[] = $row; // This is no longer used, but I'm assuming you want it for something 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);
    }
}

这应该产生一个这样的数组:

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

所以你可以通过以下方式迭代每个房间的可用日期:

foreach ($array_date as $roomNumber => $dates) {
    foreach ($dates as $date) {
        // Do something
    }
}

此解决方案可能允许日期重复输入,具体取决于表的设置方式。为了避免这些重复,您可以检查如果房间已经被保留则跳过添加日期,方法是将while中的date_range()循环的内容替换为类似

的内容
if (!in_array(date($output_format, $current), $currentDates)) { 
    $currentDates[] = date($output_format, $current);
}

$current = strtotime($step, $current);

希望这是你想要的行为。如果没有,请告诉我!