Search code examples
phparraysmergetransposechunks

Merge, chunk, then transpose and flatten data from multiple indexed arrays


I have three arrays:

$a = ['a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'a10'];
$b = ['b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8', 'b9', 'b10'];
$c = ['c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9', 'c10'];

I want to combine these arrays to create:

$new1 = (a1, a2, b1, b2, c1, c2);

$new2 = (a3, a4, b3, b4, c3, c4);

$new3 = (a5, a6, b5, b6, c5, c6);

$new4 = (a7, a8, b7, b8, c7, c8);

$new5 = (a9, a10, b9, b10, c9, c10);

How to make it like that?


Solution

  • You, you would want to map each array into it's chunked version using array_map and array_chunk, so that you get them in 2 element arrays. Then, you would want to reduce those so that each chunk goes in the same final array, for which you would use array_reduce:

    $a = array('a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'a10');
    $b = array('b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8', 'b9', 'b10');
    $c = array('c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9', 'c10');
    
    $final = array_reduce(array_map(function($el) {
                 return array_chunk($el, 2);
             }, [$a, $b, $c]), function($carry, $item) {
                 foreach($item as $key => $value) {
                     if (!isset($carry[$key])) {
                         $carry[$key] = [];
                     }
                     $carry[$key] = array_merge($carry[$key], $value);
                 }
                 return $carry;
             });
    
    $new1 = $f[0];
    $new2 = $f[1];
    $new3 = $f[2];
    $new4 = $f[3];
    $new5 = $f[4];
    
    var_dump($f);
    /*
     array(5) {
      [0]=>
      array(6) {
        [0]=>
        string(2) "a1"
        [1]=>
        string(2) "a2"
        [2]=>
        string(2) "b1"
        [3]=>
        string(2) "b2"
        [4]=>
        string(2) "c1"
        [5]=>
        string(2) "c2"
      }
      [1]=>
      array(6) {
        [0]=>
        string(2) "a3"
        [1]=>
        string(2) "a4"
        [2]=>
        string(2) "b3"
        [3]=>
        string(2) "b4"
        [4]=>
        string(2) "c3"
        [5]=>
        string(2) "c4"
      }
      [2]=>
      array(6) {
        [0]=>
        string(2) "a5"
        [1]=>
        string(2) "a6"
        [2]=>
        string(2) "b5"
        [3]=>
        string(2) "b6"
        [4]=>
        string(2) "c5"
        [5]=>
        string(2) "c6"
      }
      [3]=>
      array(6) {
        [0]=>
        string(2) "a7"
        [1]=>
        string(2) "a8"
        [2]=>
        string(2) "b7"
        [3]=>
        string(2) "b8"
        [4]=>
        string(2) "c7"
        [5]=>
        string(2) "c8"
      }
      [4]=>
      array(6) {
        [0]=>
        string(2) "a9"
        [1]=>
        string(3) "a10"
        [2]=>
        string(2) "b9"
        [3]=>
        string(3) "b10"
        [4]=>
        string(2) "c9"
        [5]=>
        string(3) "c10"
      }
    }