在php中合并后重复值到数组中

在php中合并后重复值到数组中

问题描述:

Do not understand why in my final result Shifts-5 is repeating again and again whereas in my array he is here just one time...

Thanks for help.

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/>
";
    }
}

?>

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

感谢您的帮助。 p>

http://codepad.org/XoBmnHFr p>

 &lt;?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&lt;  $ thirdArrayLength;  $ i ++)
 {
 $ currentThirdArrayValue = $ thirdArray [$ i]; 
 
 for($ inner = 0,$ firstArrayIndex = 0; $ inner&lt; $ datesCount; $ inner ++,$ firstArrayIndex ++)
 {  
 if($ firstArrayIndex == $ firstArrayLength)
 $ firstArrayIndex = 0; 
 
 echo“{$ secondArray [$ inner]} / {$ currentThirdArrayValue} / {$ firstArray [$ firstArrayIndex]}&lt; br /  &gt; 
“; 
} 
} 
 
?&gt; 
  code>  pre> 
  div>

It's because you keep resetting the pointer inside the first array at every iteration of the third array:

$firstArrayIndex=0;

Move this assignment outside of the outer for loop.

Demo

It can be done without all the counters, like this:

$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/>" ;
    }
}