title: Transpose rows and columns in a 2D array
body: <p>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:</p>
  
  <pre><code>    //Creating rows and columns for text file
      echo "&lt;h1&gt;Data Table&lt;/h1&gt;";
      echo "&lt;table border = 0 &gt;";
      foreach($result as $key=&gt;$value){
          echo "&lt;tr&gt;";
          foreach($value as $v){
              echo "&lt;td&gt;".$v."&lt;/td&gt;";
          }
          echo "&lt;/tr&gt;";
  
      }
      echo "&lt;/table&gt;";
  }
  
  function transpose_matrix($result) {
      $transpose = array(); //
      foreach ($result as $key =&gt; $sub) {
          foreach ($sub as $subkey =&gt; $subvalue) {
              $transpose[$subkey][$key] = $subvalue;
          }
      }
      return $transpose;
  }
  </code></pre>
  
  <p>My first table displays as expected and looks somthing like this:</p>
  
  <pre><code>1 2 3 4 5
  6 7 8 9 10
  </code></pre>
  
  <p>I need it to appear as such (i.e rotating the position of the rows and columns):</p>
  
  <pre><code>1 6
  2 7
  3 8
  4 9
  5 10
  </code></pre>
  
  <p>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</p>