-2

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?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136

3 Answers3

1

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"
  }
}
dave
  • 62,300
  • 5
  • 72
  • 93
0

You need add all array in one array

  1. Then applied while loop with count of $a/2
  2. Then use array_map return with iterated index value plus one .its means if is 0 its get a1,a2 value respectively .
  3. Then finally merge array and pass with result array

Sandbox Test

$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');
$arr = [$a,$b,$c];
$i=0;
$res=[];
while($i < (count($a)/2)){
    $res[$i]=call_user_func_array('array_merge',array_map(function($ar=[]) use ($i){
       return [$ar[$i],$ar[$i+1]];
    },$arr));
    $i++;
}
print_r($res);
print_r($res[0]);
$new1=$res[0];
$new2=$res[1];
$new3=$res[2];

print_r($new1);
print_r($new2);
print_r($new3);
prasanth
  • 22,145
  • 4
  • 29
  • 53
0

Here is a fully functional-style approach that will accept any number of arrays. It is assumed that your input arrays will have the same length. Because my approach is so flexible, it does not produce hardcoded, individual variables as its result.

  1. Form a single array from all input arrays, then
  2. Chunk each input array's data, then
  3. Transpose and flatten the chunks.

Code: (Demo)

var_export(
    array_map(
        fn(...$cols) => array_merge(...$cols),
        ...array_map(
            fn($row) => array_chunk($row, 2),
            [$a, $b, $c]
        )
    )
);
mickmackusa
  • 43,625
  • 12
  • 83
  • 136