PHP,Jquery获取多数组,合并一行
概念: 在酒店中添加楼层、房间数和房间号并将其保存在db中 前端设计: 请看一下这张图片 我现在得到的是:PHP,Jquery获取多数组,合并一行,php,jquery,arrays,laravel,Php,Jquery,Arrays,Laravel,概念: 在酒店中添加楼层、房间数和房间号并将其保存在db中 前端设计: 请看一下这张图片 我现在得到的是: "floor" => array:3 [▼ 0 => "3" 1 => "2" 2 => "1" ] "room_count" => array:3 [▼ 0 => "3" 1 => "3" 2 => "5" ] "room_number" => array:11 [▼
"floor" => array:3 [▼
0 => "3"
1 => "2"
2 => "1"
]
"room_count" => array:3 [▼
0 => "3"
1 => "3"
2 => "5"
]
"room_number" => array:11 [▼
0 => "101"
1 => "102"
2 => "103"
3 => "201"
4 => "202"
5 => "203"
6 => "101"
7 => "102"
8 => "103"
9 => "104"
10 => "105"
]
问题是:
如何循环获取:三楼的房间号是101、102、103
二楼的房间号是201、202、203
楼层1的房间号为101、102、103、104、105遍历每一层,计算房间数,然后通过房间号数组确定哪些房间号与该楼层相关联 考虑到您正在使用的阵列结构,诀窍是您需要有一个这样的循环,它可以通过按每层的房间数递增
$prev_index
来跟踪$prev_index
您需要根据第一个数组中楼层的值设置房间编号数组索引,否则您就没有房间和楼层的轨迹。请尝试将
行编号
,作为后缀
或前缀
分配给前端每行输入的名称,然后提交,这样您就可以拥有轨迹。您的房间号
数组索引应该具有楼层数组的值。因此,您可以根据楼层值区分房间编号。
<?php
$array = array(
'floor' => array(
0 => '3',
1 => '2',
2 => '1',
),
'room_count' => array(
0 => '3',
1 => '3',
2 => '5',
),
'room_number' => array(
0 => '101',
1 => '102',
2 => '103',
3 => '201',
4 => '202',
5 => '203',
6 => '101',
7 => '102',
8 => '103',
9 => '104',
10 => '105',
),
);
for ($prev_index = 0, $i = 0; $i < count($array['floor']); ++$i) {
$floor = $array['floor'][$i];
$room_count = $array['room_count'][$i];
$room_numbers = array_slice($array['room_number'], $prev_index, $room_count);
$prev_index = $prev_index + $room_count;
echo 'Floor '.$floor.' has '.$room_count.' rooms, with room numbers '.implode(', ', $room_numbers)."\n";
}
Floor 3 has 3 rooms, with room numbers 101, 102, 103
Floor 2 has 3 rooms, with room numbers 201, 202, 203
Floor 1 has 5 rooms, with room numbers 101, 102, 103, 104, 105