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 Answers 2
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
output[c].append(cell2)
// 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.
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);
-
1Programmers 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 compilergnat– gnat2013年11月22日 17:27:24 +00:00Commented Nov 22, 2013 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– Alastair Irvine2013年12月02日 02:24:50 +00:00Commented Dec 2, 2013 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– Florian F2014年08月22日 14:40:44 +00:00Commented Aug 22, 2014 at 14:40
rowspan
andcolspan
might be tricky, though, if you need to reconstruct the array from HTML.rowspan
andcolspan
values on every element your table should be properly reversed.