简体   繁体   中英

Php foreach loop execution

I have $student_object_array which contains information about 3 students . Then I have an array $room which contains room_id,capacity and allotment. (Note: There are 2 rooms)

$room[0]->room_id=1;
$room[1]->room_id=2;

$room[0]->capacity=2;
$room[1]->capacity=3;

$room[0]->allotment=0;
$room[1]->allotment=0;

Now I have to allot rooms to 3 students... My Code is:-

$i=0;


foreach ($student_object_array as $student_object)
{
   $capacity=$room[$i]->capacity;
   $allotment=$room[$i]->allotment;

   $allotment=$allotment_array[$i];
   if($capacity-$allotment!=0)
   {
      allotStudents($student_object->studentId, room[$i]->room_id);
   }
   else
       $i++;
}

Now, at 1st iteration capacity=2 and allotment =0 ie capacity-allotment != 0 ie 1st Student Alloted .

at 2nd iteration capacity=2 and allotment =1 ie capacity-allotment != 0 ie 2nd Student Alloted.

at 3rd iteration capacity=2 and allotment =2 ie capacity-allotment == 0 . It will go in else part and increase the value of i by 1.

So, loop executes 3 times as there are 3 students but only 2 students alloted here.... Please Help!

Try this: (updated)

 $student_object_array = [
  [
    "name" => "A",
    "room" => ""
  ],
  [
    "name" => "B",
    "room" => ""
  ],
  [
    "name" => "C",
    "room" => ""
  ]
];
$room = [
  [
    "id" => 0,
    "capacity" => 2,
    "allotment" => 0
  ],
  [
    "id" => 1,
    "capacity" => 3,
    "allotment" => 0
  ],
];

$i = 0;

//I use foreach by reference here to be able to modify objects
foreach ($student_object_array as &$student_object)
{
   $canAllot = true;

   //check if current room is full
   while ($room[$i]["capacity"] - $room[$i]["allotment"] == 0)
   {
      //if full procedd to next room until you find free
      $i++;
      if ($i >= count($room)) {
        //no free rooms left
        $canAllot = false;
        break;
      }
   }
   if ($canAllot) {
     allotStudents($student_object, $room, $i);
   } else {
     //no free rooms
     break;
   }
}

var_dump($student_object_array);
echo "<br>";
var_dump($room);

function allotStudents(&$student, &$room, $i) {
  $room[$i]["allotment"]++;
  $student["room"] = $room[$i]["id"];
}
exit();

Try this, when room allotted, maintain its record.

$i=0;

foreach ($student_object_array as $student_object)
{
   $capacity=$room[$i]->capacity;
   $allotment=$room[$i]->allotment;

   if($capacity-$allotment!=0)
   {
      $room[$i]->allotment = ++$allotment;
      allotStudents($student_object->studentId, room[$i]->room_id);
   }else{
       $i++;
       $capacity=$room[$i]->capacity;
       $allotment=$room[$i]->allotment;
       $room[$i]->allotment = ++$allotment;
       allotStudents($student_object->studentId, room[$i]->room_id);
   }
}

For reference, please see this Code sample

<?php
class Room
{
    public $room_id;
    public $capacity;
    public $allotment;  
}

class Student
{
    public $student_id;
    public $name;
}

$one             = new Room;
$one->room_id    = 1;
$one->capacity   = 2;
$one->allotment  = 0;
$two             = new Room;
$two->room_id    = 2;
$two->capacity   = 3;
$two->allotment  = 0;

$foo             = new Student;
$foo->name       = 'foo';
$foo->student_id = 1;
$bar             = new Student;
$bar->name       = 'bar';
$bar->student_id = 2;
$baz             = new Student;
$baz->name       = 'baz';
$baz->student_id = 3;

$rooms           = [$one, $two];
$students        = [$foo, $bar, $baz];

$i = 0;
$associations = [];
foreach($students as $student) {
    $room = $room->allotment == $room->capacity
        ? $rooms[++$i]
        : $rooms[$i];
    $associations[] =
        [
            'student_id' => $student->student_id,
            'room_id'    => $room->room_id
        ];
    $room->allotment++;
}
var_export($associations);
var_export($rooms);

Output:

array (
  0 => 
  array (
    'student_id' => 1,
    'room_id' => 1,
  ),
  1 => 
  array (
    'student_id' => 2,
    'room_id' => 1,
  ),
  2 => 
  array (
    'student_id' => 3,
    'room_id' => 2,
  ),
)array (
  0 => 
  Room::__set_state(array(
     'room_id' => 1,
     'capacity' => 2,
     'allotment' => 2,
  )),
  1 => 
  Room::__set_state(array(
     'room_id' => 2,
     'capacity' => 3,
     'allotment' => 1,
  )),
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM