gpt4 book ai didi

php - 合并具有相同键的数组的子数组

转载 作者:行者123 更新时间:2023-11-29 12:17:30 24 4
gpt4 key购买 nike

我正在尝试填充一个数组,其中 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 的数组?

最佳答案

我认为第二个示例实际上并不是您想要的结构 - 如果您只是想存储每个房间号的日期,则不需要零索引的第一维度,因此该维度只是碍事。我认为最好的方法是通过引用函数传递现有的子数组,以便它可以直接将元素添加到数组中。我重写了您的代码(并且还删除了一些冗余/低效率),例如额外的循环,以产生我认为您想要的内容:

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

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

关于php - 合并具有相同键的数组的子数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29545756/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com