我看到有一个可接受的答案,但我想我会发布我的答案,因为我昨晚开始研究这个问题,今天早上想通了......
在在线解析器上查看 3v4l:https://3v4l.org/O8hOh
使用颜色来设置表格样式: https://3v4l.org/ZUl5p
获取数组并使用foreach()进行迭代,然后使用rand()创建一个新数组$checks,然后检查值!in_array() && !array_key_exists()是否存在于该新数组中,如果不存在,则我们迭代计数器并将新值和玩家姓名推送到新数组中。我们将这一切包装在 while 循环中,检查计数器是否等于 count($arr) --> 游戏玩家的数量,从数组 count() 动态提取。
我们使用第二个条件来确保新数组count($check) === count($arr) 是相同的数字,然后将团队推进到key=NULL 或team=NULL 的原始数组中。 foreach() 循环构造$gamers 数组。将原始值推回新数组,然后有条件地检查是否可被 2 整除,因为每次调用时,$checks 数组将始终为玩家数量随机分配一个新数字,if($check[$value['name']] % 2 == 0) 分配团队 2 --> $gamers[$key]['team'] = 2;。
或$gamers[$key]['team'] = 'blue';、else{ $gamers[$key]['team'] = 1 } 或else{ $gamers[$key]['team'] = 'Red' };
原来保存有NULL 键值team 的玩家的旧数组现在保存新分配的团队。
基本代码在代码 cmets 中有解释:
$arr = array ( 0 => array ( 'ID' => '3', 'name' => 'olaf', 'team' => NULL, ), 1 => array ( 'ID' => '4', 'name' => 'Peter', 'team' => NULL, ), 2 => array ( 'ID' => '5', 'name' => 'chris', 'team' => NULL, ), 3 => array ( 'ID' => '6', 'name' => 'günther', 'team' => NULL, ), );
// count the number of player in the array and assign to variable
$numOfPlayers = count($arr);
// initiate an empty array to hold the values of the players random unique numbers
$check = array();
$i = 0; // use a counter to evaluate whether number of players set in array is met
while($i < count($arr)){ // iterate through foreach loop until we have met the number of players using while loop
// iterate through each time the while loop fires to get the $value
foreach($arr as $key => $value){
// create a random number between 0 and the number of players present in array
// to check and pass into new array if is not set yet
$random = rand(1,$numOfPlayers);
// conditional that check if the random number is in the new array, if not we push that as a value into the new array
// we also check if the persons name is set as a key, if not, we push that as a key into the new aray
if(!in_array($random, $check) && !array_key_exists($value['name'], $check)){
// set the new key/value pairs and iterate the counter for the while loop
$check[$value['name']] = $random;
$i++;
}
}
}
// Now see if the two arrays $check and $arr are equal in count
if(count($check) === count($arr)){
// now assign teams using modulus
foreach($arr as $key => $value){
// construct the old array with original values
$gamers[$key] = $value;
// if value is divisible by 2, assign to specific team change operator here
// if you want to swap what team get the odd numbered players
if($check[$value['name']] % 2 == 0){
$gamers[$key]['team'] = 2;
// else if not divisible by two assign to other team
}else{
$gamers[$key]['team'] = 1;
}
}
}
作为一个函数:
function constTeams($arr){
$numOfPlayers = count($arr);
$check = array();
$i = 0;
while($i < count($arr)){
foreach($arr as $key => $value){
$random = rand(1,$numOfPlayers);
if(!in_array($random, $check) && !array_key_exists($value['name'], $check)){
$check[$value['name']] = $random;
$i++;
}
}
}
if(count($check) === count($arr)){
foreach($arr as $key => $value){
$gamers[$key] = $value;
if($check[$value['name']] % 2 == 0){
$gamers[$key]['team'] = 'Blue';
}else{
$gamers[$key]['team'] = 'Red';
}
}
}
return $gamers;
}
var_dump(constTeams($arr)); 使用上述函数的输出,每次调用都会改变:
array(4) {
[0]=>
array(3) {
["ID"]=>
string(1) "3"
["name"]=>
string(4) "olaf"
["team"]=>
string(3) "Red"
}
[1]=>
array(3) {
["ID"]=>
string(1) "4"
["name"]=>
string(5) "Peter"
["team"]=>
string(4) "Blue"
}
[2]=>
array(3) {
["ID"]=>
string(1) "5"
["name"]=>
string(5) "chris"
["team"]=>
string(4) "Blue"
}
[3]=>
array(3) {
["ID"]=>
string(1) "6"
["name"]=>
string(8) "günther"
["team"]=>
string(3) "Red"
}
}
与奇数玩家数组一起使用:
$players = array (
0 => array ( 'ID' => '3', 'name' => 'olaf', 'team' => NULL, ),
1 => array ( 'ID' => '4', 'name' => 'Peter', 'team' => NULL, ),
2 => array ( 'ID' => '5', 'name' => 'chris', 'team' => NULL, ),
3 => array ( 'ID' => '6', 'name' => 'günther', 'team' => NULL, ),
4 => array ( 'ID' => '7', 'name' => 'John', 'team' => NULL, ),
5 => array ( 'ID' => '8', 'name' => 'Jack', 'team' => NULL, ),
6 => array ( 'ID' => '9', 'name' => 'Bob', 'team' => NULL, ),
7 => array ( 'ID' => '10', 'name' => 'Jake', 'team' => NULL, ),
8 => array ( 'ID' => '11', 'name' => 'Bill', 'team' => NULL, )
) ;
$output = '
<table>
';
foreach(constTeams($players) as $key => $value){
$output .= '
<tr border="1">
<td>'.$value['name'].'</td>
<td>Team: '.$value['team'].'</td>
</tr>';
}
$output .= '
</table>';
输出:每次调用函数时随机变化。
注意:奇数球员将由操作员在模数方程条件下确定,更改它以更改哪支球队获得过强的球员人数。 == 或 != --> 这里:if($check[$value['name']] % 2 == 0)