0

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

?>
Francois
  • 15
  • 5
  • What exactly are you trying to do with your code? I want to know, so I understand your problem/issue. – bestprogrammerintheworld May 05 '13 at 14:17
  • 1
    Because you're iterating `$firstarray` again and again when doing `$firstArrayIndex = 0;` .. – Nelson May 05 '13 at 14:18
  • can you give idea of expected output .... – Baba May 05 '13 at 14:18
  • Ok. First array if for the day the user need to make (for example Shifts-41 is from 9am to 5pm). Second array is to know wich date are in the current view/table. The third one is for user. I know that in one week there's 7 days. My first array got 42 datas. 42/7=6. I have 6 users. I want for each date, for each user, each type of day he needs to make. With it solution Elvena was so close but there a trouble with day type if there are not the same for users. – Francois May 05 '13 at 14:18
  • For expecting result see my comment pls: http://codepad.org/XoBmnHFr#comment-LidycMfX Thanks. – Francois May 05 '13 at 14:20
  • Why do you have so many duplicate `Shifts-1` in your shift data – Baba May 05 '13 at 14:24
  • Cause most of users in my table make this type od hours (Shifts-1 = 9am to 5pm). http://www.send-picture.com/upload/Screen-Shot-2013-05-04-at-5.24.46-PM.jpg – Francois May 05 '13 at 14:27
  • @Francois: Why are you posting the [same question](http://stackoverflow.com/questions/16377935/merge-multiple-array-depending-loop-and-number) again and [again](http://stackoverflow.com/questions/16384857/merge-3-differents-arrays) ? – hakre May 05 '13 at 14:47
  • Well, what do you think?! Did you see I've delete one of them and that's this one is not exactly the same? – Francois May 05 '13 at 15:30
  • @Francois: I'm only relating to what you wrote here your own: *"With it solution Elvena was so close"* - you referenced it so I ask you. – hakre May 05 '13 at 16:10

2 Answers2

2

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/>" ;
    }
}
hakre
  • 193,403
  • 52
  • 435
  • 836
Dirk McQuickly
  • 2,099
  • 1
  • 17
  • 19
0

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

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309