首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Php循环执行

Php循环执行
EN

Stack Overflow用户
提问于 2018-03-12 11:26:50
回答 3查看 97关注 0票数 1

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

代码语言:javascript
复制
$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;

现在我得给三个学生分配房间..。我的准则是:-

代码语言:javascript
复制
$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,即容量-分配!= 0,即第一学生分配

在第二次迭代 capacity=2和分配=1,即容量-分配!= 0,即第二学生分配。

在第三次迭代 capacity=2和分配=2即容量-分配== 0.它将进入其他部分并将i的值增加1.

因此,循环执行3次,因为有3学生,但只有2学生分配给这里.请帮帮忙!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-03-12 11:38:58

试试这个:(更新)

代码语言:javascript
复制
 $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();
票数 0
EN

Stack Overflow用户

发布于 2018-03-12 12:10:10

尝试这个,当空间分配时,保持它的记录。

代码语言:javascript
复制
$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);
   }
}

如需参考,请参阅此代码样本

票数 0
EN

Stack Overflow用户

发布于 2018-03-12 13:19:11

代码语言:javascript
复制
<?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);

输出:

代码语言:javascript
复制
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,
  )),
)
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49234085

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档