将3个数组与列格式(PHP)组合在一起
I have 3 arrays like this format
$array1 = array(a,b,c,d);
$array2 = array(e,f,g,h);
$array3 = array(i,j,k,l);
I need to merge this 3 arrays with column format
My expected result
array(a,e,i,b,f,j,c,g,k,d,h,l);
How can i do this. I am already do array merge. But its merge row format. but i need column format.
Please Advise
Updated
$array1 = array(a,b,c,d,x,u,xx);
$array2 = array(e,f,g,h,s,d,t);
$array3 = array(i,j,k,l);
我有3个像这种格式的数组 p>
$ array1 = array(a,b,c,d);
$ array2 = array(e,f,g,h);
$ array3 = array(i,j,k,l);
code>
我需要将这3个数组与列格式合并 p>
我的预期结果 p>
array( a,e,i,b,f,j,c,g,k,d,h,l);
code> pre>
我该怎么做。 我已经做了数组合并。 但它的合并行格式。 但我需要列格式。 p>
请告知 p>
已更新 strong> h1>
$ array1 = array(a,b,c,d,x,u,xx);
$ array2 = array(e,f,g,h,s,d,t);
$ array3 = array (i,j,k,l);
code> pre>
div>
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);
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);
Try this
$sorted array = sort(array_merge( (array) $array1, (array) $array2 , (array) $array3)
found the answer in here.
use this function as bellow, no matter how many elements in each array this will work.
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 )