Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 分配一个没有余数的数字_Php_Arrays_Division - Fatal编程技术网

Php 分配一个没有余数的数字

Php 分配一个没有余数的数字,php,arrays,division,Php,Arrays,Division,我想在不切割多余部分的情况下,将一个项目分配到特定的部分, 例如: $persons=7; $rooms=2; 对于($i=0;$i0) // { //$the_room[$index++]++; // } 打印室(房间); 如果您希望按顺序共享剩余部分,请添加“顺序分发”代码块。 $persons = 7; $rooms = 2; $the_room = array(); for($i = 0; $i < $rooms; $i++) { $the_room[$i] = floor(

我想在不切割多余部分的情况下,将一个项目分配到特定的部分, 例如:

$persons=7;
$rooms=2;
对于($i=0;$i<$rooms;$i++)
{
//分配每个房间的人数
}
//端点应该是
$the_room[0]=4//而不是3.5
$the_room[1]=3//而不是3.5

一种先平均分配片段,然后随机分配剩余片段的方法怎么样

$persons = 7;
$rooms = 2;
$the_room = array();

for($i = 0; $i < $rooms; $i++)
{
  $the_room[$i] = floor($persons / $rooms);
}

// Random Distribution
while( $persons - array_sum($the_room) > 0)
{
  $the_room[array_rand($the_room)]++;
}

// Sequential Distribution
// $index = 0;
// while( $persons - array_sum($the_room) > 0)
// {
//   $the_room[$index++]++;
// }

print_r($the_room);
$persons=7;
$rooms=2;
$the_room=array();
对于($i=0;$i<$rooms;$i++)
{
$the_room[$i]=楼层($persons/$rooms);
}
//随机分布
而($persons-array\u sum($the\u room)>0)
{
$the_room[array_rand($the_room)]++;
}
//顺序分布
//$index=0;
//而($persons-array\u sum($the\u room)>0)
// {
//$the_room[$index++]++;
// }
打印室(房间);
如果您希望按顺序共享剩余部分,请添加“顺序分发”代码块。
$persons = 7;
$rooms = 2;
$the_room = array();

for($i = 0; $i < $rooms; $i++)
{
  $the_room[$i] = floor($persons / $rooms);
}

// Random Distribution
while( $persons - array_sum($the_room) > 0)
{
  $the_room[array_rand($the_room)]++;
}

// Sequential Distribution
// $index = 0;
// while( $persons - array_sum($the_room) > 0)
// {
//   $the_room[$index++]++;
// }

print_r($the_room);