1

I have 3 flat, indexed arrays.

$array1 = ['a', 'b', 'c', 'd'];
$array2 = ['e', 'f', 'g', 'h'];
$array3 = ['i', 'j', 'k', 'l'];

I need to merge this 3 arrays by their column position.

My expected result:

['a', 'e', 'i', 'b', 'f', 'j', 'c', 'g', 'k', 'd', 'h', 'l'];

I have already tried array_merge(), but the order of the elements does not meet my needs.


Update

My input arrays may have different lengths from one another.

$array1 = ['a', 'b', 'c', 'd', 'x', 'u', 'xx'];
$array2 = ['e', 'f', 'g', 'h', 's', 'd', 't'];
$array3 = ['i', 'j', 'k', 'l'];
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Padmanathan J
  • 4,614
  • 5
  • 37
  • 75
  • Multiple answers on [Transpose and flatten two-dimensional indexed array where rows may not be of equal length](https://stackoverflow.com/q/25669227/2943403) are perfectly capable of accommodating more than 2 input arrays (as well as an unknown number of arrays). – mickmackusa Sep 04 '22 at 08:53

3 Answers3

2

If the 3 arrays have the same length, then:

$result = array();
$count = count($array1);
for ($i = 0; $i < $count; $i++)
{
    $result[] = $array1[$i];
    $result[] = $array2[$i];
    $result[] = $array3[$i];
}
var_export($result);

-

                    ---------------------------------- 

If the arrays do not have the same length:

$result = array();
$count = max(count($array1), count($array2), count($array3));
for ($i = 0; $i < $count; $i++)
{
    isset($array1[$i]) && $result[] = $array1[$i];
    isset($array2[$i]) && $result[] = $array2[$i];
    isset($array3[$i]) && $result[] = $array3[$i];
}
var_export($result);
srain
  • 8,944
  • 6
  • 30
  • 42
1

PHP 5.5 solution:

$array1 = array('a', 'b', 'c', 'd');
$array2  = array('e', 'f', 'g', 'h');
$array3  = array('i', 'j', 'k', 'l');

$composite = array($array1, $array2, $array3);
$count = count($array1);
$result = array();
for ($i = 0; $i < $count; $i++) {
    $result = array_merge($result, array_column($composite, $i));
}
var_dump($result);

EDIT

or another alternative that will work with versions of PHP before 5.5

$array1 = array('a','b','c','d');
$array2  = array('e','f','g','h');
$array3  = array('i','j','k','l');

$composite = array($array1, $array2, $array3);
// transpose
array_unshift($composite, null);
$tmpResult = call_user_func_array('array_map', $composite);
// flatten
$result = call_user_func_array('array_merge', $tmpResult);
var_dump($result);

EDIT #2

Using this second method, if the initial arrays are varying lengths you will get "padded" NULL entries in the final result; these can be removed if they're not wanted by using array_filter();

$array1 = array('a','b','c','d','x','u','xx');
$array2 = array('e','f','g','h','s','d','t');
$array3 = array('i','j','k','l');

$composite = array($array1, $array2, $array3);
// transpose
array_unshift($composite, null);
$tmpResult = call_user_func_array('array_map', $composite);
// filter and flatten
$result = array_filter(
    call_user_func_array('array_merge', $tmpResult),
    function ($value) {
        return $value !== NULL;
    }
);
var_dump($result);
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • i like your 2 nd answer. but i have one doubt. My 3 arrays are different values i mean 1 st array getting 2 values and second array 5 values and 3rd array 4 values. How can i use your second result – Padmanathan J Aug 22 '13 at 12:59
1

No matter how many elements in each array this will work.

var_export(
    array_interlace($array1, $array2, $array3)
);

function array_interlace() {
    $args = func_get_args();
    $total = count($args);

    if($total < 2) {
        return FALSE;
    }
   
    $i = 0;
    $j = 0;
    $arr = array();
   
    foreach($args as $arg) {
        foreach($arg as $v) {
            $arr[$j] = $v;
            $j += $total;
        }
       
        $i++;
        $j = $i;
    }
   
    ksort($arr);
    return array_values($arr);
} 

Results for updated question :

Array ( [0] => a [1] => e [2] => i [3] => b [4] => f [5] => j [6] => c [7] => g [8] => k [9] => d [10] => h [11] => l [12] => x [13] => s [14] => u [15] => d [16] => xx [17] => t )
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Janith Chinthana
  • 3,792
  • 2
  • 27
  • 54