There is a two-dimensional array describing a HTML table. Each element of the array consists of:

  • the cell content
  • rowspan
  • colspan

Every row of this two dimensional array corresponds to <td> cells of a <tr> of the table which my software should generate.

I need to "reverse" the array (interchange vertical and horizontal direction).

Insofar I considered algorithm based on this idea: make a rectangular matrix of the size of the table and store in every element of this matrix the corresponding index of the element of the above mentioned array. (Note that two elements of the matrix may be identical due rowspan/colspan.) Then I could use this matrix to calculate rowspan/colspan for the inverted table.

But this idea seems bad for me. Any other algorithms?

Note that I program in PHP.

  • 2
    Your idea seems sound. The operation you're performing is called transposing a matrix. Handling rowspan and colspan might be tricky, though, if you need to reconstruct the array from HTML. – LSerni Nov 6 '13 at 23:00
  • I suppose that if, after you 'reverse' the array, you swap the rowspan and colspan values on every element your table should be properly reversed. – xpy Nov 24 '13 at 19:20
  • @xpy: sadly it is more complex than this – porton Nov 24 '13 at 22:45

The only difficulty is to find on which column a cell starts. Once you figure how to do that you can collect the cells for each column and output them as a row, exchanging colspan and rowspan. To find the starting colum you have to track what part of the table positions is used by previous cells. Each cell starts at the next available position.

I don’t know php, so I will use pseudocode.

The input is an array that gives for each row the list of variable-sized cells. The output is in the same format.

I will assume the table fills a rectangular area. If not, some padding with empty cells will be neessary in the output.

function reverse( table:array of array of cell ) 
  used = new array of array of boolean.
  output = new array of array of cell

  for r = 0 to table.size()-1 {
    // process row r
    c = 0;
    for each cell in table[r] {
      // skip to next position
      while used[r][c] { c++; }
      // cell starts at column c.  add it to the output
      cell2 = cell with rowspan and colspan values switched
      // mark used table positions (use 1 for missing rowspan/colspan)
      for i = 0 to cell.rowspan-1 {
        for j = 0 to cell.colspan-1 {
          used[r+i][c+j] = true;

  return output;

I hope you can translate it to php.

| improve this answer | |

array_column() will take the nth element of every subarray in a multidimensional array and produce an array from that "column". If you do this multiple times for increasing index n, you can construct a new multidimensional array where each of the rows is a column from the input array. The example below accounts for the fact that not all of the subarrays might be of the same length, and uses the length of the longest one as the number of columns (to avoid losing data). (Explanation added during edit.)

// Assumes source array is $input

// find the "longest" subarray
$max = 0;
foreach ($input as $subarray)
  $max = max($max, count($subarray));

// generate a transposed output array
$output = array();
for ($i = 0; $i < $max; ++$i)
  $output[] = array_column($input, $i);

See http://www.php.net/manual/en/function.array-column.php

| improve this answer | |
  • 1
    Programmers is tour conceptual questions and I expect answers to explain things. Throwing code dumps instead of explanation is like copying code from IDE to whiteboard: it may look familiar and even sometimes be understandable, but it feels weird... just weird. Whiteboard doesn't have compiler – gnat Nov 22 '13 at 17:27
  • I don't see the OP saying he can't follow the code. It's commented and uses only one "difficult" library function. Therefore your comment is not constructive. – Alastair Irvine Dec 2 '13 at 2:24
  • This is a simple transposition. The problem is that is does not take care of the size of the cells. It works only with simple tables of 1x1 cells. – Florian F Aug 22 '14 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.