php - Populate a multidimensional array of rooms with another multidimensional array of names -
i'm trying assign people $prides
1 multidimensional array rooms $campus
in multidimensional array.
the people in first array first grouped coded arrays indicating building preference, room preference, , gender.
i need first match appropriate arrays, i.e. arrays of people large rooms in building bldgalargef
, bldgalargem
. fill each room until run out of females, leave remaining bunks empty, , populate next room males. if run out of rooms before run out of people, remaining people need placed homeless array.
the number of rooms fixed, number of people, or number of groups in people array can vary.
how write without creating loopy mess or building hundreds of nested if , case statements?
<?php /* - multidimensional array of people - group requirements: building, room size, smoking, roommate smoking preference - multidimensional array of rooms - group building, size, bunks - break arrays sets - count number of people in each set - reserve block of required rooms - assign remaining people 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 match sizes of pride arrays $keys = array_keys($prides); $iterations = count($array[$keys[0]]); foreach($campus $building => $buildings) { foreach($buildings $size => $sizes) { $y=0; foreach($sizes $roomno => $room) { $thisroomdesc = $building . $size; switch($thisroomdesc){ case "acblarge": $bunks = count($room); for($x = 0; $x < $bunks; $x++){ $room[$x] = $prides["acblargef"][$y] . "\r"; // isn't working. } break; } } } } print_r($campus); print_r($keys); echo $iterations; ?>
i this:
- work
$prides
array , remove every person gets room. in end homeless people remaining in array. - use
$campus
array reference , fill directly.
foreach($campus $building => &$buildings) { foreach($buildings $size => &$sizes) { foreach($sizes $roomno => &$room) { // build pride string of room $roompride = $building . $size; // check if there pride of room defined - if not: continue next room if(!array_key_exists($roompride.'f',$prides) && !array_key_exists($roompride.'m',$prides)) continue; // determine gender room $roomgender = (!empty($prides[$roompride.'f'])) ? 'f' : 'm'; // loop on room size foreach($room &$space) { // check weather there people left fit room's "settings" if(!empty($prides[$roompride.$roomgender])) { // if so, place people in , remove them $prides array $space = $prides[$roompride.$roomgender][0]; array_splice($prides[$roompride.$roomgender], 0, 1); } } } } } // flatten $prides array homeless people $homeless = call_user_func_array('array_merge', $prides);
Comments
Post a Comment