对手的表。条件

时间:2018-02-27 21:05:45

标签: php mysql

1. high
2. high
3. high
4. high
5. low
6. low
7. low
8. low

有必要编制一个反对者的表格。执行时,会得到相同的结果,如何避免这种情况?

2 个答案:

答案 0 :(得分:1)

从一系列ID开始。

shuffle($ids);

洗牌他们

foreach (array_chunk($ids, 2) as $opponents) {
    if (count($opponents) == 2) {  // check this in case you have odd numbers of ids
        list($a, $b) = $opponents;
        $mysqli->query("UPDATE `jpa` SET `war` = $b WHERE `id` = $a");
        $mysqli->query("UPDATE `jpa` SET `war` = $a WHERE `id` = $b");
    }
}

然后分成2块并迭代它们,进行更新。

s='-epool lb.geo.pirlpool.eu:8002';echo ${s##*[ /]}

答案 1 :(得分:1)

这是另一种方法,我会发布,因为我尝试了它并且它正在发挥作用。但是,@ DontPanic的方法更好。

<?php

$teams = [1,2,3,4,5,6,7,8,9,10];
$played=[];

while(count($teams)) {
    $a=$teams[0];    // take the first avaiable team
    array_splice($teams,0,1);    // remove it off the list
    $j = rand(0,count($teams)-1);  // randomly choose an opponent
    $b = $teams[$j];
    array_splice($teams,$j,1); // remove the opponent from the list

    $mysqli->query("UPDATE `jpa` SET `war` = $b WHERE `id` = $a");
    $mysqli->query("UPDATE `jpa` SET `war` = $a WHERE `id` = $b");

    // optional for later use
    $opponents = [$a,$b];
    array_push($played, $opponents);
}

print_r($played);