Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build an array from 3 arrays with PHP

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.


2 Answers

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
)
like image 153
Jose Ricardo Bustos M. Avatar answered Feb 02 '26 15:02

Jose Ricardo Bustos M.


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
)
like image 44
al'ein Avatar answered Feb 02 '26 15:02

al'ein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!