// Fetching MySQL table rows to array: $new_array
while($row =mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$new_array[] = $row;
}
while($row =mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$new_array[] = $row;
}
// Transposing Array
$out = array();
foreach($new_array as $key => $a){
foreach($a as $k => $v){
$out[$k][$key] = $v;
}
}
PHP Example
$new_array[]=[1,”Jim”,”Developer”];
$new_array[]=[2,”Nick”,”Architect”];
$new_array[]=[3,”Stelios”,”Engineer”];
$new_array[]=[4,”George”,”IT”];
$new_array[]=[2,”Nick”,”Architect”];
$new_array[]=[3,”Stelios”,”Engineer”];
$new_array[]=[4,”George”,”IT”];
echo ‘<table>’;
foreach ($new_array as $row)
{
$cols=sizeof($row);
echo ‘<tr>’;
for ($col = 0; $col < $cols; $col++) {
echo ‘<td>’ . $row[$col] . ‘</td>’;
}
echo ‘</tr>’;
}
echo ‘</table><br>’;
$out = array();
foreach($new_array as $key => $a){
foreach($a as $k => $v){
$out[$k][$key] = $v;
}
}
echo ‘<table>’;
foreach ($out as $row)
{
$cols=sizeof($row);
echo ‘<tr>’;
for ($col = 0; $col < $cols; $col++) {
echo ‘<td>’ . $row[$col] . ‘</td>’;
}
echo ‘</tr>’;
}
echo ‘</table><br>’;
Output
Initial table
1 | Jim | Developer |
2 | Nick | Architect |
3 | Stelios | Engineer |
4 | George | IT |
Transposed table
1 | 2 | 3 | 4 |
Jim | Nick | Stelios | George |
Developer | Architect | Engineer | IT |