2015-05-15 129 views
1

我试图将人$prides从一个多维数组分配给另一个多维数组中的房间$campus使用另一个多维数组名称填充多维数组阵列

第一个数组中的人首先被分组为编码数组,指示他们的建筑物偏好,房间偏好和性别。

我需要首先匹配适当的数组,即获得建筑物中大房间的人数组,这将是bldgaLargeFbldgaLargeM。然后填满每个房间,直到我用完女性,剩下的床铺留空,然后用男性填充隔壁房间。如果我在用完人之前耗尽了房间,剩下的人需要被安置到无家可归的人群中。

房间数量是固定的,人数或甚至人数组中的组数可能会有所不同。

我该如何写这个而不会创建一个恶作剧或建立数百个嵌套的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; 
?> 
+0

你有机会获得像MySQL数据库?这将极大地简化您的学生的订购和分配属性 –

+0

将不同的属性分配给各个学生可能有意义,就像使用数据库表一样;所以它会像骄傲[“studentX”] =>数组(“房间大小”=>“小”,“性别”=>“女性”)...等等,以 –

+0

开头为什么你有一个concat \评论说它不起作用? – gladiola

回答

0

我可能会做这样的:与$prides阵列

  • 工作,并删除每个人是进入一个房间。所以最终无家可归的人仍然是这个阵列中的人。
  • 通过引用使用$campus数组,并直接填充它。
foreach($campus as $building => &$buildings) { 
    foreach($buildings as $size => &$sizes) { 
     foreach($sizes as $roomno => &$room) { 
      // build the pride string of this room 
      $roomPride = $building . $size; 
      // check if there is the pride of this room defined - if not: continue with the next room 
      if(!array_key_exists($roomPride.'F',$prides) && !array_key_exists($roomPride.'M',$prides)) continue; 
      // determine the gender for this room 
      $roomGender = (!empty($prides[$roomPride.'F'])) ? 'F' : 'M'; 
      // loop over the room size 
      foreach($room as &$space) { 
       // check weather there are people left that fit this room's "settings" 
       if(!empty($prides[$roomPride.$roomGender])) { 
        // if so, place the people in and remove them from the $prides array 
        $space = $prides[$roomPride.$roomGender][0]; 
        array_splice($prides[$roomPride.$roomGender], 0, 1); 
       } 
      } 
     } 
    } 
} 
// flatten the $prides array to get all homeless people 
$homeless = call_user_func_array('array_merge', $prides); 
+0

这就像一个魅力!非常感谢你的帮助。 – gaspero1