0

不明白为什么在我的最终结果中Shifts-5一次又一次地重复,而在我的数组中他只在这里一次......

感谢帮助。

http://codepad.org/XoBmnHFr

<?php

$firstArray = array("Leaves-19", "Shifts-5", "Shifts-1", "Shifts-1", "Shifts-1", "Shifts-1", "Leaves-19", "Leaves-19", "Shifts-1", "Shifts-1", "Shifts-1", "Shifts-1", "Shifts-1", "Leaves-19", "Leaves-19", "Shifts-1", "Shifts-1", "Shifts-1", "Shifts-1", "Shifts-1", "Leaves-19", "Leaves-19", "Shifts-1", "Shifts-1", "Shifts-1", "Shifts-1", "Shifts-1", "Leaves-19", "Leaves-19", "Shifts-1", "Shifts-1", "Shifts-1", "Shifts-1", "Shifts-1", "Leaves-19", "Leaves-19", "Shifts-1", "Shifts-1", "Shifts-1", "Shifts-1", "Shifts-1", "Leaves-19");

$secondArray = array("2013-04-28", "2013-04-29", "2013-04-30", "2013-05-01", "2013-05-02", "2013-05-03", "2013-05-04");

$thirdArray = array("13", "10", "12", "9", "14", "11");


$datesCount        = count( $secondArray );
$firstArrayLength  = count( $firstArray );
$thirdArrayLength  = count( $thirdArray );

for( $i=0 ; $i < $thirdArrayLength ; $i++ )
{
    $currentThirdArrayValue = $thirdArray[$i];

    for( $inner=0, $firstArrayIndex=0 ; $inner < $datesCount ; $inner++, $firstArrayIndex++ )
    {
        if( $firstArrayIndex == $firstArrayLength )
                $firstArrayIndex = 0;

        echo "{$secondArray[$inner]} / {$currentThirdArrayValue} / {$firstArray[$firstArrayIndex]}<br/>\n";
    }
}

?>
4

2 回答 2

2

它可以在没有所有计数器的情况下完成,如下所示:

$secondArray = array("2013-04-28", "2013-04-29", "2013-04-30", "2013-05-01", "2013-05-02", "2013-05-03", "2013-05-04");

$thirdArray = array("13", "10", "12", "9", "14", "11");

$secondArrayLength = count($secondArray);

foreach($thirdArray as $third_i => $third_value)
{
    foreach($secondArray as $sec_i => $sec_value)
    {
        $first_value = $firstArray[($third_i * $secondArrayLength) + $sec_i];
        echo "$sec_value / $third_value / $first_value<br/>" ;
    }
}
于 2013-05-05T16:10:55.787 回答
0

这是因为您在第三个数组的每次迭代中不断重置第一个数组内的指针:

$firstArrayIndex=0;

将此分配移到外for循环之外。

演示

于 2013-05-05T14:49:38.683 回答