Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to merge 3 arrays.

My first one is:

Array ( [0] => Leaves-19 [1] => Shifts-1 [2] => Shifts-1 [3] => Shifts-1 [4] => Shifts-1 [5] => Shifts-1 [6] => Leaves-19 [7] => Leaves-19 [8] => Shifts-1 [9] => Shifts-1 [10] => Shifts-1 [11] => Shifts-1 [12] => Shifts-1 [13] => Leaves-19 [14] => Leaves-19 [15] => Shifts-1 [16] => Shifts-1 [17] => Shifts-1 [18] => Shifts-1 [19] => Shifts-1 [20] => Leaves-19 [21] => Leaves-19 [22] => Shifts-1 [23] => Shifts-1 [24] => Shifts-1 [25] => Shifts-1 [26] => Shifts-1 [27] => Leaves-19 [28] => Leaves-19 [29] => Shifts-1 [30] => Shifts-1 [31] => Shifts-1 [32] => Shifts-1 [33] => Shifts-1 [34] => Leaves-19 [35] => Leaves-19 [36] => Shifts-1 [37] => Shifts-1 [38] => Shifts-1 [39] => Shifts-1 [40] => Shifts-1 [41] => Leaves-19 )

My second is:

Array ( [0] => 2013-04-28 [1] => 2013-04-29 [2] => 2013-04-30 [3] => 2013-05-01 [4] => 2013-05-02 [5] => 2013-05-03 [6] => 2013-05-04 )

The third one is:

Array ( [0] => 13 [1] => 10 [2] => 12 [3] => 9 [4] => 14 [5] => 11 )

I want:

  • 2013-04-28 / 13 / Leaves-19
  • 2013-04-29 / 13 / Shifts-1
  • 2013-04-30 / 13 / Shifts-1
  • 2013-05-01 / 13 / Shifts-1
  • 2013-05-02 / 13 / Shifts-1
  • 2013-05-03 / 13 / Shifts-1
  • 2013-05-04 / 13 / Leaves-19
  • 2013-04-28 / 10 / Leaves-19
  • 2013-04-29 / 10 / Shifts-1
  • 2013-04-30 / 10 / Shifts-1
  • 2013-05-01 / 10 / Shifts-1
  • 2013-05-02 / 10 / Shifts-1
  • 2013-05-03 / 10 / Shifts-1
  • 2013-05-04 / 10 / Leaves-19
  • ...

Thansk for help.


What I tryed:

echo print_r($_POST['dayType'])."<hr />";
echo print_r($_POST['dayArr'])."<hr />";
echo print_r($_POST['userArr'])."<hr />";

//echo count($_POST['dayType'])." --- ".count($_POST['dayArr'])." --- ".count($_POST['userArr']);

// 2013-05-04 / 13 / Leaves-19
$loopNb1 = count($_POST['dayType']);
$loopNb2 = count($_POST['dayType'])/7;

for($a=0; $a<$loopNb1; $a++) {
    echo $_POST['dayType'][$a]."<br />";
}

echo "<hr />";

for($b=0; $b<$loopNb2; $b++) {
    echo $_POST['userArr'][1]."<br />";
}
share|improve this question
2  
It would be nice to post what you have tried. –  HamZa May 4 at 19:08
 
Your third array has only 6 elements, not 7. –  PhpMyCoder May 4 at 19:09
 
Thanks. I put an example. Think it's better for understanding now. –  Francois May 4 at 19:11
 
You might be looking for php.net/array_map, see Example #4 Creating an array of arrays –  hakre May 4 at 19:11
 
Thanks for links but every examples shows egual arrays. Mines are not eqal. –  Francois May 4 at 19:21
add comment

marked as duplicate by Jon, hakre, hjpotter92, Baba, TheHippo May 5 at 17:04

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

3 Answers

up vote 1 down vote accepted

First/second/third arrays are the same that you pasted (Same order).

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

Will give you:

2013-04-28 / 13 / Leaves-19<br/>
2013-04-29 / 13 / Shifts-1<br/>
2013-04-30 / 13 / Shifts-1<br/>
2013-05-01 / 13 / Shifts-1<br/>
2013-05-02 / 13 / Shifts-1<br/>
2013-05-03 / 13 / Shifts-1<br/>
2013-05-04 / 13 / Leaves-19<br/>
2013-04-28 / 10 / Leaves-19<br/>
2013-04-29 / 10 / Shifts-1<br/>
2013-04-30 / 10 / Shifts-1<br/>
2013-05-01 / 10 / Shifts-1<br/>
2013-05-02 / 10 / Shifts-1<br/>
2013-05-03 / 10 / Shifts-1<br/>
2013-05-04 / 10 / Leaves-19<br/>

etc... ending with:

2013-05-01 / 11 / Shifts-1<br/>
2013-05-02 / 11 / Shifts-1<br/>
2013-05-03 / 11 / Shifts-1<br/>
2013-05-04 / 11 / Leaves-19<br/>
share|improve this answer
 
I really need to make like in my example. :( –  Francois May 4 at 19:44
 
Hm, I see what you mean, the fix should be to loop over the dates array and just check if the other two are going to be above their last item and reset the array. -- Want me to modify it? –  elvena May 4 at 19:54
 
It would be so appreciated please. Thanks. –  Francois May 4 at 19:55
 
Sure, a simple clarification, before your first "..." is 2013-05-04 / 13 / Leaves-19, which is the next value you expect? –  elvena May 4 at 20:03
 
Yes, I just put ... to separate them. :) Thanks. –  Francois May 4 at 20:05
show 5 more comments

Get an idea-

//Your array
$a = array(1, 2, 3, 4, 5, 6,7,8);
$b = array("one", "two", "three", "four", "five",'123', 'asdfsadf');
$c = array("uno", "dos", "tres", "cuatro", "cinco", 'jina');


function myFunc($first, $second, $third) {
    if (!empty($first) && !empty($second) && !empty($second)) {
        return $first . '/' . $second . '/'. $third;
    }
}

// get the lowest size of array
$minimumSize = min(count($a), count($b), count($c));

//modify array in terms of lowest size
$firstArray = array_slice($a, 0, $minimumSize);
$secondArray = array_slice($b, 0, $minimumSize);
$thirdArray = array_slice($c, 0, $minimumSize);

$d = array_map('myFunc', $firstArray, $secondArray, $thirdArray);
share|improve this answer
 
Thanks for your help but my arrays aren't equal. –  Francois May 4 at 19:26
 
Edited the code. –  smm May 4 at 20:09
add comment

If you want all combinations from those 3 arrays (call them $first, $second, $third), you can do something like this:

foreach($first as $a){
 foreach($second as $b){
   foreach($third as $c{
     //do whatever you want with $a,$b,$c
     echo "$a/$b/$c";
   }
 }  
} 
share|improve this answer
 
Thanks but I need to keep relation between informations. Elvena code work like a charm but not for the 1st array if datas are not the same between users, Do your understand? Could your please help? Thanks. –  Francois May 5 at 13:22
 
does this data by any chance come from a database? Isn't it better to solve the problem in the database instead of PHP? –  Axarydax May 5 at 13:23
 
This is to save this sort of table send-picture.com/upload/… –  Francois May 5 at 13:25
 
I really don't understand the connection between the first array (which has indices from [0] => Leaves-19 [1] => Shifts-1 ... [40] => Shifts-1 [41] => Leaves-19) what does the index 40 and 41 mean for example? –  Axarydax May 5 at 13:32
1  
Ok. First array array 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 5 at 13:42
show 1 more comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.