使用自制mod函数索引数组的问题

时间:2013-12-05 00:17:10

标签: php arrays associative-array modulo

这是其中一个问题,其中每个小部分似乎都按照你的意愿行事,但他们不会一起工作。

大图:我正在进行一项实验,其中每个网页的新访问者都会自动分配到六个条件中的一个(我们将其称为" A"通过&#34 ; F&#34)。我只需要一种方法来跟踪到目前为止有多少人访问过,并使用该信息来计算分配给他们的组。例如,第一个访客将处于条件状态" A",第6个条件" F",然后第7个回到条件" A"再一次,等等。换句话说,只是一个简单的mod函数。

在我的真实代码中,我通过文本文件和会话变量的组合跟踪访问者编号和组分配。我已经从这里抽象出来了(请不要让我使用全局变量),但是在这两种情况下都会出现同样的问题:经过一次很好地踩过阵列后,我开始得到不稳定的输出不明白。更令人费解的是,当我自己看每一步时,一切都排好了。在第8次通过,奇怪的开始。让我带你走过。

下面的第一行(" SESSION的输出[' GroupAssignment']是2")正确表明我们想要从下面数组的[2] nd索引中拉出来它。快速浏览一下这个数组会告诉你 - 正确地说 - [2] nd条目是" C"。最后一个数组,只有一个条目,是代码的结果,基本上等于$ Array [2] [0]。在任何合理的宇宙中,都会回归" C"。但正如你所看到的,它会返回" B"。

The output of SESSION['GroupAssginment'] is 2

Array
(
    [0] => Array
        (
            [0] => A
        )

    [1] => Array
        (
            [0] => B
        )

    [2] => Array
        (
            [0] => C
        )

    [3] => Array
        (
            [0] => D
        )

    [4] => Array
        (
            [0] => E
        )

    [5] => Array
        (
            [0] => F
        )

)
Array
(
    [0] => B
)
The text file for subject 8 will be B.

这种事情一再发生(在相同的数字上可靠),但我无法找出任何可辨别的来源或模式。我希望你可以!代码如下:

<?
$Group = array(//structured like this 'cause my real experiment reads these from text files in a way that results in this structure.
            array("A"),
            array("B"),
            array("C"),
            array("D"),
            array("E"),
            array("F")
            );

$NumberOfGroups = count($Group);

for ($i=0; $i<48; $i++){ //current iteration ~ subject number
    CalculateMod($i, $NumberOfGroups);//given the current iteration, figure out which entry (0-5) you want to use.
    global $GroupAssignment_Outer;
    AssignGroup($i,$Group,$GroupAssignment_Outer);//go grab the right file from the inner array using the appropriate outer index.
}

function CalculateMod($value, $precision){//this part works perfectly. 

    $remainder = ($value/$precision - round($value/$precision));
    if ($remainder < 0){
        $remainder = $remainder+1;
    }
    global $GroupAssignment_Outer;
    $GroupAssignment_Outer = $remainder*$precision;
}

function AssignGroup($subjectnumber, $group, $outerindex){//the error must be somewhere in here... but where!?
    $GroupAssignment_Inner = $group[$outerindex][0];
    echo "The text file for subject " . $subjectnumber .  " will be " . $GroupAssignment_Inner . "<br>"; 
}

?>

如果有帮助,请输入上面的代码:

The text file for subject 0 will be A
The text file for subject 1 will be B
The text file for subject 2 will be C
The text file for subject 3 will be D
The text file for subject 4 will be E
The text file for subject 5 will be F
The text file for subject 6 will be A
The text file for subject 7 will be B
The text file for subject 8 will be B
The text file for subject 9 will be D
The text file for subject 10 will be E
The text file for subject 11 will be F
The text file for subject 12 will be A
The text file for subject 13 will be A
The text file for subject 14 will be C
The text file for subject 15 will be D
The text file for subject 16 will be D
The text file for subject 17 will be F
The text file for subject 18 will be A
The text file for subject 19 will be A
The text file for subject 20 will be C
The text file for subject 21 will be D
The text file for subject 22 will be D
The text file for subject 23 will be F
The text file for subject 24 will be A
The text file for subject 25 will be B
The text file for subject 26 will be B
The text file for subject 27 will be D
The text file for subject 28 will be E
The text file for subject 29 will be E
The text file for subject 30 will be A
The text file for subject 31 will be B
The text file for subject 32 will be B
The text file for subject 33 will be D
The text file for subject 34 will be E
The text file for subject 35 will be E
The text file for subject 36 will be A
The text file for subject 37 will be B
The text file for subject 38 will be B
The text file for subject 39 will be D
The text file for subject 40 will be E
The text file for subject 41 will be E
The text file for subject 42 will be A
The text file for subject 43 will be B
The text file for subject 44 will be B
The text file for subject 45 will be D
The text file for subject 46 will be E
The text file for subject 47 will be E

1 个答案:

答案 0 :(得分:2)

我怀疑......

  

//此部分完美运作

有一个mod运算符%

for ($i=0; $i<48; $i++){
    AssignGroup($i,$Group,$i % $NumberOfGroups);
}