7

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

Aaron Martin
  • 128
  • 1
  • 13
  • https://stackoverflow.com/questions/797251/transposing-multidimensional-arrays-in-php – ewcz Dec 08 '17 at 16:45

4 Answers4

4

This should give you what you need.

function transpose($array_one) {
    $array_two = [];
    foreach ($array_one as $key => $item) {
        foreach ($item as $subkey => $subitem) {
            $array_two[$subkey][$key] = $subitem;
        }
    }
    return $array_two;
}

Then just pipe your existing array into the function and render the resulting array.

Bryant Makes Programs
  • 1,493
  • 2
  • 17
  • 39
2

Check this: https://3v4l.org/OnuSu

$table = [
    [1, 2, 3, 4, 5],
    [6, 7, 8, 9, 10],
];

$i         = 0;
$transpose = [];
while ($columns = array_column($table, $i++))
{
    $transpose[] = $columns;
}

$table = '<table border="1">';
$rows = count($transpose);
for ($i = 0; $i < $rows; $i++){
    $cols = count($transpose[$i]);
    $table .= '<tr>';
    for ($j = 0; $j < $cols; $j++)
    {
        $table .= '<td>' . $transpose[$i][$j] . '</td>';
    }
    $table .= '</tr>';
}
$table .= '</table>';
echo $table;
Mauricio Florez
  • 1,112
  • 8
  • 14
1

You can just extract the columns of the sub-arrays one-by-one:

foreach(reset($result) as $key => $values) {
    $transpose[] = array_column($result, $key);
}

However this is probably better and should work even if the keys are different in each array:

$transpose = array_map(null, ...$result);
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
0

Your function seems to be working fine you just need to call it.

Add this below the function:

//Creating rows and columns for text file
$result[0] = ['1','2','3','4','5'];
$result[1] = ['6','7','8','9','10'];
$result[2] = ['11','12','13','14','15'];
$result[3] = ['16','17','18','19','20'];
$result[4] = ['21','22','23','24','25'];
    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;
}

$mat = transpose_matrix($result);
echo "<h1>Data Table</h1>";
    echo "<table border = 0 >";
    foreach($mat as $key=>$value){
        echo "<tr>";
        foreach($value as $v){
            echo "<td>".$v."</td>";
        }
        echo "</tr>";

    }
    echo "</table>";
Alam Darji
  • 275
  • 3
  • 13
  • No... After the closing bracket( return $transpose; }) After this line. – Alam Darji Dec 08 '17 at 17:06
  • Thanks I tried using that code but unfortunately it's still not displaying correctly, here is a screenshot of the output: https://imgur.com/a/0D5tK ... The rows appear to be displaying correctly but with no columns. The first value of the second row in the first table (26) appears to be showing after the first row has been transposed (as seen in the second table display at (24 26) row 20) – Aaron Martin Dec 08 '17 at 17:12
  • updated answer please look. – Alam Darji Dec 08 '17 at 17:19
  • Still having the same problem. I am using numbers from an external text file – Aaron Martin Dec 08 '17 at 17:22
  • How are the numbers stored in your text file? – Alam Darji Dec 08 '17 at 18:09
  • the numbers in the text file just look something like 26, 3, 54, 23, 45, 23, 65, etc; I have used strpos and substr to remove any commas "," between each number and I have stored these numbers in a 2D array named $result – Aaron Martin Dec 08 '17 at 18:20
  • can you provide me the data in $result so I can apply changes accordingly? – Alam Darji Dec 08 '17 at 18:33