Php foreach循环执行

时间:2018-03-12 11:26:51

标签: php

我有 $ student_object_array ,其中包含 3名学生的相关信息。 然后我有一个数组 $ room ,其中包含room_id,容量和分配。  (注:有2个房间)

$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;

现在我必须给3名学生分配房间......我的代码是: -

$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++;
}

现在,  第一次迭代   capacity = 2和分配= 0,即capacity-outotment!= 0,即第一名学生分配

第二次迭代

  capacity = 2和分配= 1,即capacity-outotment!= 0,即2nd All Allased。

第三次迭代

  capacity = 2和分配= 2,即capacity-outotment == 0。 它会进入其他部分,并将i的值增加1。

所以,循环执行 3次,因为 3名学生但只有 2名学生在这里分配了......请帮助!

3 个答案:

答案 0 :(得分:0)

试试这个:(更新)

 $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();

答案 1 :(得分:0)

在分配房间时,请尝试这样做,保持其记录。

$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);
   }
}

供参考,请参阅此Code sample

答案 2 :(得分:-1)

<?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);

输出:

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,
  )),
)