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

1

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

replacing $array_one with $result?
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;

3 Comments

Why not just while ($transpose[] = array_column($table, $i++)){}
The last position in $transpose will be empty because the assign in array_column before the check on the while
Ahhh, good catch.
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);

3 Comments

hey thanks, should I replace the foreach statement in my function?
If this works and your foreach doesn't then yes.
How are you "outputting" it?
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>";

7 Comments

No... After the closing bracket( return $transpose; }) After this line.
Thanks I tried using that code but unfortunately it's still not displaying correctly, here is a screenshot of the output: 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)
updated answer please look.
Still having the same problem. I am using numbers from an external text file
How are the numbers stored in your text file?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.