php - Fill array with values from different other arrays from different lengths -


i'm trying following: examns, have x chairs , x pupils. chairs varie , people varie per class , year.

example: there 112 chairs , 102 pupils. need fill chairs pupils of same year not next each other.

the chairs not in rows, places varie too.

what did:

  • i've got 2 arrays columns of chairs: $chairs1: a, d, f,... , $chairs2=b, e, g,... (i allready filtered row c wich empty example) way, can put third year in array 1 , fourth in array 2. way, pupils first year not next each other
  • i have 1 array number of pupils per year. $numberofpupilsperyear[1] = number of pupils in first year.
  • i have 7 other arrays, 1 each year text: $class-$examn-$classnumber. fyi: 3wb-english-1, 3wb-english-2, 3wb-english-3,...
  • all chairs numberd a1 r19. not chairs exists in every place. have array $allchairs[] existing chairs in particular room check if chair exists.

now need fill biggest chairarray biggest yeararray. if $chairs1 has 80 chairs, , year 2 has pupils, filling should begin in $chairs1, pupils year 2, going on second pupils per year.

what have untill now, plus of above.

if($chairs1 >= $chairs2)     {     //fill chairs1     $currentnumber = 1;     $counter=0;     while ($currentnumber < 20)      {            foreach($chairs1 $key => $value)          {                //make coordinate of chair             $currentletter = $value;             //check if chair exists             if(in_array($currentletter.$currentnumber, $allchairs))              {                 //fill place pupil example: 3wb-english-1                  //but i'm bit stuck here...                 //i trying following                 $biggestyear = $number_of_students[$counter];                 echo "biggestyear: ".$biggestyear;                 ${$classlist.$biggestyear};              }         }         $currentnumber++;     }     //now fill chairs2     else     {         //fill chairs2 first , chairs1     } 

i hope understand mean... , if need rethink whole pseudocode in beginning, please provide hints.

if i'm correct in nutshell:

what try accomplish given grid of chairs (whether or not grid equally sized), assign pupils based on class, same class isn't next eachother.

chair numbers / names, class numbers , exams "metadata" in case , not add towards real problem.

so, start off first designing grid of chairs, must possible block off chairs (i.e. assign "not available" example).

since im better code words:

$grid = new chairgrid(6, 18);  // set non-available chairs $grid->get(2,5)->assigned = 'na'; $grid->get(1,10)->assigned = 'na';  // change names wish $grid->get(0,12)->name = 'bg1';  print($grid);  $pupils = array(     1 => 20,     2 => 30,     3 => 20,     4 => 20,     5 => 20 );  // returns left-overs var_dump($grid->assign($pupils));  print($grid); 

chargrid class:

class chairgrid {     private $sizex;     private $sizey;     private $grid;      public function __construct($sizex, $sizey) {         $this->grid = array();          $this->sizex = $sizex;         $this->sizey = $sizey;          for($x = 0; $x < $sizex; $x++) {             $this->grid[$x] = array();             for($y = 0; $y < $sizey; $y++) {                 $chair = new chair(chr(ord('a') + $y) . $x, $x, $y);                 $this->grid[$x][$y] = $chair;             }         }     }      /**      * @param $x      * @param $y      *      * @return chair      */     public function get($x, $y) {         if (isset($this->grid[$x][$y])) {             return $this->grid[$x][$y];         }          return null;     }      public function assign($pupils) {         $retval = array();          foreach($pupils $class=>$number) {             $retval[$class] = 0;              $column = 0;             $row = -1;             for($i=0; $i < $number;$i++) {                 $done = false;                 while(!$done) {                     $row++;                     if ($row >= $this->sizey) {                         $row = 0;                         $column++;                         if ($column >= $this->sizex) {                             $retval[$class]++;                             break;                         }                     }                      $chair = $this->get($column, $row);                     if ($chair instanceof chair && $chair->assigned === null) {                         // var_dump($chair->getx() . ',' . $chair->gety() . ' --?--> '.$class);                          $chairleft = $this->get($chair->x - 1, $chair->y);                         $chairright = $this->get($chair->x + 1, $chair->y);                         if ($chairright === null) {                             $chairfirst = $this->get(0, $chair->y);                             if ($chairleft === null || $chairleft->assigned !== $class) {                                 // assign                                 $chair->assigned = $class;                                 $done = true;                             }                             else if ($chairfirst->assigned !== $class) {                                 $prev = null;                                 for($movecolumn = $this->sizex - 1; $movecolumn >= 0; $movecolumn--) {                                     $current = $this->get($movecolumn, $row);                                     if ($current->assigned !== 'na') {                                         if ($prev instanceof chair) {                                             $prev->assigned = $current->assigned;                                         }                                          $prev = $current;                                     }                                 }                                 // cannot assign, check "right" in first column, if can assign, shift column                                 $chairfirst->assigned = $class;                                 $done = true;                             }                         }                         else if (($chairleft === null || $chairleft->assigned !== $class) && ($chairright === null || $chairright->assigned !== $class)) {                             $chair->assigned = $class;                             $done = true;                         }                     }                 }             }         }          return $retval;     }      public function __tostring() {         $string = array();         for($y = 0; $y < $this->sizey; $y++) {             $tmp = '';             for($x = 0; $x < $this->sizex; $x++) {                 $tmp .= '[' . str_pad($this->grid[$x][$y]->name, 3, ' ', str_pad_right) . '|' . str_pad($this->grid[$x][$y]->assigned, 3, ' ', str_pad_right) . ']';             }             $string[] = $tmp;         }          return implode("\n", $string) . "\n";     } } 

simple chair class:

class chair {     public $name;     public $assigned = null;      public $x;     public $y;      public function __construct($name, $x, $y) {         $this->name = $name;         $this->x = $x;         $this->y = $y;     } } 

Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -