Hard to put a descriptive title to this which is easy to understand so I will try in more depth here. Say I have 4 arrays of different sizes, a1, a2, a3 and a4 which I want to combine into the array SUM.
I want SUM[0] to be a1[0], SUM[1] to be a2[0], SUM[2] to be a3[0], SUM[3] to be a4[0] and then SUM[4] to be a1[1] and so on.
A tricky thing to consider is the arrays are different sizes.
edit: if any array a1,a2,a3,a4 terminates, just move onto the next one - sorry for not making this clear
$a1=array(1,2,3);
$a2=array(4,5,6,7,8,9);
$a3=array(10,11);
$a4=array(12,13,14,15);
$maxlen = max(count($a1), count($a2), count($a3), count($a4));
$a = array($a1, $a2, $a3, $a4);
$SUM = array();
for ($i = 0; $i < $maxlen; $i++) {
foreach ($a as $arr) {
if (array_key_exists($i, $arr)) {
$SUM[] = $arr[$i];
}
}
}
var_dump($SUM);
OUTPUT:
array(15) { [0]=> int(1) [1]=> int(4) [2]=> int(10) [3]=> int(12) [4]=> int(2) [5]=> int(5) [6]=> int(11) [7]=> int(13) [8]=> int(3) [9]=> int(6) [10]=> int(14) [11]=> int(7) [12]=> int(15) [13]=> int(8) [14]=> int(9) }