Reputation: 4620
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'];
Upvotes: 1
Views: 1636
Reputation: 3844
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 )
Upvotes: 1
Reputation: 212412
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);
Upvotes: 1
Reputation: 9082
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);
Upvotes: 2