I have created a table that displays a series of numbers in a table. I am trying to transpose the matrix (flip rows and columns) using a for each loop and a function named transpose_matrix but it doesnt seem to be working for me. Where am I going wrong with this? I am working with the following code:
//Creating rows and columns for text file
echo "<h1>Data Table</h1>";
echo "<table border = 0 >";
foreach($result as $key=>$value){
echo "<tr>";
foreach($value as $v){
echo "<td>".$v."</td>";
}
echo "</tr>";
}
echo "</table>";
}
function transpose_matrix($result) {
$transpose = array(); //
foreach ($result as $key => $sub) {
foreach ($sub as $subkey => $subvalue) {
$transpose[$subkey][$key] = $subvalue;
}
}
return $transpose;
}
My first table displays as expected and looks somthing like this:
1 2 3 4 5
6 7 8 9 10
I need it to appear as such (i.e rotating the position of the rows and columns):
1 6
2 7
3 8
4 9
5 10
I have searched StackOverflow for similar questions or solutions but cannot seem to find one that works. I am fairly new to PHP also so apologies if it is a simple fix