我试图将人$prides
从一个多维数组分配给另一个多维数组中的房间$campus
。使用另一个多维数组名称填充多维数组阵列
第一个数组中的人首先被分组为编码数组,指示他们的建筑物偏好,房间偏好和性别。
我需要首先匹配适当的数组,即获得建筑物中大房间的人数组,这将是bldgaLargeF
和bldgaLargeM
。然后填满每个房间,直到我用完女性,剩下的床铺留空,然后用男性填充隔壁房间。如果我在用完人之前耗尽了房间,剩下的人需要被安置到无家可归的人群中。
房间数量是固定的,人数或甚至人数组中的组数可能会有所不同。
我该如何写这个而不会创建一个恶作剧或建立数百个嵌套的if和case语句?
<?php
/*
- multidimensional array of people
- group by requirements: building, room size, smoking, roommate smoking preference
- multidimensional array of rooms
- group by building, size, bunks
- break arrays into sets
- count number of people in each set
- reserve block of required rooms
- assign remaining people to homeless array
*/
$prides = array(
"acbLargeM" => array("Matt","Jason","Tim","Jeffrey","Ed","Jim"),
"acbLargeF" => array("Andrea","Ashley","Renae","Dena","Amanda","Amie","Angie"),
"acbSmallM" => array("Eric","Matt"),
"acbSmallF" => array(),
"tbnLargeM" => array("Bill","David","David","Eric"),
"tbnLargeF" => array("Dawn","Linda","Heather","Heidi"),
"tbnSmallM" => array("Joe","Keith","Michael","Jeff","Jack","Michael","Ronn","Tony"),
"tbnSmallF" => array("Melanie","Melissa","Tara","Victoria"));
$campus = array(
"acb" => array(
"Large" => array(
"101" => array("", "", "", ""),
"103" => array("", "", "", ""),
"105" => array("", "", "", ""),
"107" => array("", "", "", ""),
"109" => array("", "", "", ""),
"111" => array("", "", "", "")
),
"Small" => array(
"102" => array("", ""),
"104" => array("", ""),
"106" => array("", ""),
"108" => array("", ""),
"110" => array("", ""),
"112" => array("", "")
)),
"tbn" => array(
"Large" => array(
"101" => array("", "", "", ""),
"103" => array("", "", "", ""),
"105" => array("", "", "", ""),
"107" => array("", "", "", ""),
"109" => array("", "", "", ""),
"111" => array("", "", "", "")
),
"Small" => array(
"102" => array("", ""),
"104" => array("", ""),
"106" => array("", ""),
"108" => array("", ""),
"110" => array("", ""),
"112" => array("", "")
))
);
$homeless = array();
// count prides
$acbLargeMcount = count($prides['acbLargeM']);
$acbLargeFcount = count($prides['acbLargeF']);
$acbSmallMcount = count($prides['acbSmallM']);
$acbSmallFcount = count($prides['acbSmallF']);
$tbnLargeMcount = count($prides['tbnLargeM']);
$tbnLargeFcount = count($prides['tbnLargeF']);
$tbnSmallMcount = count($prides['tbnSmallM']);
$tbnSmallFcount = count($prides['tbnSmallF']);
// build room arrays that match sizes of pride arrays
$keys = array_keys($prides);
$iterations = count($array[$keys[0]]);
foreach($campus as $building => $buildings) {
foreach($buildings as $size => $sizes) {
$y=0;
foreach($sizes as $roomno => $room) {
$thisRoomDesc = $building . $size;
switch($thisRoomDesc){
case "acbLarge":
$bunks = count($room);
for($x = 0; $x < $bunks; $x++){
$room[$x] = $prides["acbLargeF"][$y] . "\r";
// this isn't working.
}
break;
}
}
}
}
print_r($campus);
print_r($keys);
echo $iterations;
?>
你有机会获得像MySQL数据库?这将极大地简化您的学生的订购和分配属性 –
将不同的属性分配给各个学生可能有意义,就像使用数据库表一样;所以它会像骄傲[“studentX”] =>数组(“房间大小”=>“小”,“性别”=>“女性”)...等等,以 –
开头为什么你有一个concat \评论说它不起作用? – gladiola