i am trying to loop through a two-dimensional array and take a sum of the combinations of the columns automatically.
Suppose i have an array called $a with 4 columns:0,1,2,3,
$a=array();
$a[0][0]=1;
$a[0][1]=3;
$a[0][2]=5;
$a[1][0]=10;
$a[1][1]=2;
$a[1][2]=5;
$a[1][3]=7;
$a[2][0]=9;
$a[2][1]=8;
$a[2][2]=9;
$a[2][3]=8;
$a[3][0]=9;
$a[3][1]=8;
$a[3][2]=9;
$a[3][3]=8;
$a[3][4]=1;
And i am trying to sum over all of the combinations of the columns like sum(0,0;1,0;2;0,3;0) etc using this code
for($i=0;$i<count($a[0]);$i++){
for($l=0;$l<count($a[1]);$l++){
for($s=0;$s<count($a[2]);$s++){
for($m=0;$m<count($a[3]);$m++){
echo $sum[]= $a[0][$i]+$a[1][$l]+$a[2][$s]+$a[3][$m];
echo $sum;
echo "<br>";
}
}
}
}
?>
And the code works, the problem is that i am doing these for loops manually, there must be some way in which i can simplify this by somehow inserting the count of the number of columns?
I tried something like
$numberofcolumns=4;
for($n=0;$n<$numberofcolumns;$n++){
for($i=0;$i<count($a[$n]);$i++){
for($m=0;$m<count($a[$n+1]);$m++){
echo $sums[]= $a[$n][$i]+$a[$n+1][$m];
}
}
}
but that doesn't work, there must be some way to simplify the for loops so that i don't have to manually type in the for loops each column
anybody have a clue?