3

I've some problem to construct an array.

Array A:

Array
(
    [0] => 2015-09-13
    [1] => 2015-09-14
    [2] => 2015-09-15
    [3] => 2015-09-16
    [4] => 2015-09-17
    [5] => 2015-09-18
    [6] => 2015-09-19
)

Array B:

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

Array C:

Array
(
    [0] => Leaves-19
    [1] => Shifts-18
    [2] => Shifts-18
    [3] => Shifts-18
    [4] => Shifts-18
    [5] => Shifts-18
    [6] => Leaves-19
    [7] => Leaves-19
    [8] => Shifts-12
    [9] => Shifts-12
    [10] => Shifts-12
    [11] => Shifts-12
    [12] => Shifts-12
    [13] => Leaves-19
)

Desired final output:

Array
(
    [0] => 2015-09-13|1|Leaves-19
    [1] => 2015-09-14|1|Shifts-18
    [2] => 2015-09-15|1|Shifts-18
    [3] => 2015-09-16|1|Shifts-18
    [4] => 2015-09-17|1|Shifts-18
    [5] => 2015-09-18|1|Shifts-18
    [6] => 2015-09-19|1|Leaves-19
    [7] => 2015-09-13|8|Leaves-19
    [8] => 2015-09-14|8|Shifts-12
    [9] => 2015-09-15|8|Shifts-12
    [10] => 2015-09-16|8|Shifts-12
    [11] => 2015-09-17|8|Shifts-12
    [12] => 2015-09-18|8|Shifts-12
    [13] => 2015-09-19|8|Leaves-19
)

I'm lost in for and foreach.

Here's the logic:

  • 1st parameter is a date and it come's form array B. It is repeat after 6 entries.
  • 2nd parameter is the user id. It changes after 6 entries and pass to the next id.
  • 3rd parameter is an entry of array B.

Oter informations:

  • The arrays don't have the same length.
  • Array A, counts 6 entries.
  • Array B, counts a random entries.
  • Array C, is Array A x 2.

I already tried to make a for for my array B and after a foreach in array A, but it wasn't functional.

I do not know where I need to start.

Hope I will have any help or cue.

Thanks a lot.

4
  • 2
    You need to explain the logic of how the arrays are related, and what you've actually tried so far. "I do not know where I need to start" is not really much of a research effort. Commented Sep 14, 2015 at 18:06
  • Is array C always the same length of the final array? And is the final array always count(A) * count(B) in length? Commented Sep 14, 2015 at 18:08
  • @rjdown: Question edited. Hope it will help. Commented Sep 14, 2015 at 18:11
  • Do the array size will remain same?? Commented Sep 14, 2015 at 18:16

6 Answers 6

6

you can use modulus operator

$OutputArray = Array();

for($i=0; $i < max(count($a1),count($a2),count($a3)); $i++){
  array_push($OutputArray, $a1[ $i % count($a1) ] . "|" . 
             $a2[ $i % count($a2) ] . "|" . $a3[ $i % count($a3) ]);
}

print_r($OutputArray);

you get:

Array
(
    [0] => 2015-09-13|1|Leaves-19
    [1] => 2015-09-14|8|Shifts-18
    [2] => 2015-09-15|1|Shifts-18
    [3] => 2015-09-16|8|Shifts-18
    [4] => 2015-09-17|1|Shifts-18
    [5] => 2015-09-18|8|Shifts-18
    [6] => 2015-09-19|1|Leaves-19
    [7] => 2015-09-13|8|Leaves-19
    [8] => 2015-09-14|1|Shifts-12
    [9] => 2015-09-15|8|Shifts-12
    [10] => 2015-09-16|1|Shifts-12
    [11] => 2015-09-17|8|Shifts-12
    [12] => 2015-09-18|1|Shifts-12
    [13] => 2015-09-19|8|Leaves-19
)

if you want in order (expected):

$OutputArray = Array();

$max = max(count($a1),count($a2),count($a3));
for($i=0; $i < $max; $i++){
  array_push($OutputArray, $a1[$i%count($a1)] . "|" . 
             $a2[ $i*count($a2) / $max ] . "|" . $a3[$i%count($a3)]);
}

print_r($OutputArray);

you get:

Array
(
    [0] => 2015-09-13|1|Leaves-19
    [1] => 2015-09-14|1|Shifts-18
    [2] => 2015-09-15|1|Shifts-18
    [3] => 2015-09-16|1|Shifts-18
    [4] => 2015-09-17|1|Shifts-18
    [5] => 2015-09-18|1|Shifts-18
    [6] => 2015-09-19|1|Leaves-19
    [7] => 2015-09-13|8|Leaves-19
    [8] => 2015-09-14|8|Shifts-12
    [9] => 2015-09-15|8|Shifts-12
    [10] => 2015-09-16|8|Shifts-12
    [11] => 2015-09-17|8|Shifts-12
    [12] => 2015-09-18|8|Shifts-12
    [13] => 2015-09-19|8|Leaves-19
)
Sign up to request clarification or add additional context in comments.

15 Comments

It didn't seem to do what the OP needed at the Array B part.
@AlanMachado actually it does, you get the same values just in a different sort order.
Look like it works. Only one thing: I normally must have 13 entries. With your code I code 14. So I just remove the equal sign.
That was a fine algorithm, sir! I was working toward a similar end, but you can type faster! lol.
@ABC The desired output in your question SHOWS 14 elements! I would upvote this answer again, if I could!
|
2

Try this, it iterates over the biggest array and condition the counting of the minor ones to its desired listing behavior.

<?php

$arrA = ['2015-09-13','2015-09-14','2015-09-15','2015-09-16',
        '2015-09-17','2015-09-18','2015-09-19'];
$arrB = [1,8];
$arrC = ['Leaves-19','Shifts-18','Shifts-18','Shifts-18','Shifts-18','Shifts-18',
'Leaves-19','Leaves-19','Shifts-12','Shifts-12','Shifts-12','Shifts-12','Shifts-12',
'Leaves-19'];
$a = $b = 0;

for ($c = 0; $c < count($arrC); $c++) {
    $arrC[$c] = $arrA[$a].'|'.$arrB[$b].'|'.$arrC[$c];
    $a++;
    if ($a == count($arrA)) {
        $a = 0;
        $b++;
    }
};

echo "<pre>";
print_r($arrC);

OUTPUT:

Array
(
    [0] => 2015-09-13|1|Leaves-19
    [1] => 2015-09-14|1|Shifts-18
    [2] => 2015-09-15|1|Shifts-18
    [3] => 2015-09-16|1|Shifts-18
    [4] => 2015-09-17|1|Shifts-18
    [5] => 2015-09-18|1|Shifts-18
    [6] => 2015-09-19|1|Leaves-19
    [7] => 2015-09-13|8|Leaves-19
    [8] => 2015-09-14|8|Shifts-12
    [9] => 2015-09-15|8|Shifts-12
    [10] => 2015-09-16|8|Shifts-12
    [11] => 2015-09-17|8|Shifts-12
    [12] => 2015-09-18|8|Shifts-12
    [13] => 2015-09-19|8|Leaves-19
)

Comments

0

My version:

$arrayA = array('2015-09-13','2015-09-14','2015-09-15','2015-09-16','2015-09-17','2015-09-18','2015-09-19');
$arrayB = array(1,8);
$arrayC = array('Leaves-19','Leaves-18','Leaves-17','Leaves-16','Leaves-15','Leaves-14','Leaves-13','Leaves-12','Leaves-11','Leaves-10','Leaves-9','Leaves-8','Leaves-7','leaves-6');
$output = array();
$count = 0;

foreach($arrayB as $arrayBElement){
    foreach($arrayA as $arrayAElement){
        $output[] = $arrayC[$count] . '|' . $arrayAElement . '|' . $arrayBElement;
        $count++;
    }
}

var_dump($output);

E: removed erroneous count

1 Comment

Edit and keep array C at the end, B in middle and A at starting
0

This stinks of a homework problem so I'm going to answer in pseudocode so hopefully you can apply some hints and come to the solution yourself.

It appears that maybe Array B ([1,8]) is a list of indexes at which to start printing out the next index. Your own statement (2nd parameter is the user id. It changes after 6 entries and pass to the next id.) is not accurate based on your desired final output:

[6] => 2015-09-19|1|Leaves-19
[7] => 2015-09-13|8|Leaves-19

This moves to the next id at index 7 (count 8) not index 6 (count 7, or one after 6 entries).

So My pseudo solution would be this:

  • Create a new array by repeating Array A until it matches the length of Array C
  • Append Array B to Array A by finding the element of Array B that is equal to or less than (1+index):

    0 => '1', 1 => '1' ... 7 => '8'

  • Concatenate Array C to every array with index parity (eg. newArray[ 0 ] . ArrayC[ 0 ])

This gives you the final output you desire.

Comments

0

try this code, I have named your 3 arrays as $arrA, $arrB and $arrC

$arrA = Array(
  '2015-09-13',
  '2015-09-14',
  '2015-09-15',
  '2015-09-16',
  '2015-09-17',
  '2015-09-18',
  '2015-09-19'
);
$arrB = array(1,8);
$arrC  = array(
  'Leaves-19',
  'Shifts-18',
  'Shifts-18',
  'Shifts-18',
  'Shifts-18',
  'Shifts-18',
  'Leaves-19',
  'Leaves-19',
  'Shifts-12',
  'Shifts-12',
  'Shifts-12',
  'Shifts-12',
  'Shifts-12',
  'Leaves-19'
);

$output = array();
$indx = 0;
foreach($arrB as $b){
    foreach($arrA as $a){
        $output[] = $a.'|'.$b.'|'.$arrC[$indx];
        $indx++;/*new line addded*/
    }
    //$indx++;/*remove this line from here*/
}

echo "<pre>";
print_r($output);
echo "</pre>";

3 Comments

Do you think Array C gets incremented every step?
Not really what it was excepted about the 3rd parameter.
And where is the 3rd parameter Shifts-12. :(
-1

With different sizes try guess maxlength

//Create arrays
var Adates= ["2015-09-13", "2015-09-14", "2015-09-15"];
var Bnums= ["1", "2", "3"];
var Cnums= ["Leaves-19", "Shifts-18", "Shifts-18"];

//get size
var maxLenght=0;
if(Adates.length>maxLenght) maxLenght=Adates.length;
if(Bnums.length>maxLenght) maxLenght=Bnums.length;
if(Cnums.length>maxLenght) maxLenght=Cnums.length;

//merge and populate
var OutputArray;
for (i = 0; i < maxLenght; i++) {
      OutputArray[i]=Adates[i]+"|"+Bnums[i]+"|"+Cnums[i];
}

1 Comment

is this javascript? I believe OP is targeting PHP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.