<?php
// hierarchy / non-hierarchical table <=> hierarchical table
function get_hierarchical_table_from_non_hierarchical_table( $t ) {
/* USE:
$t = array(
array( 'a', 'a', 'a' ),
array( 'a', 'a', 'b' ),
array( 'a', 'b', 'b' ),
array( 'b', 'b', 'b' )
);
$r = get_hierarchical_table_from_non_hierarchical_table( $t );
*/
/* RESULT:
$r = array(
array( 'a', 'a', 'a' ),
array( '', '', 'b' ),
array( '', 'b', 'b' ),
array( 'b', 'b', 'b' )
);
*/
/* VIEW:
a a a
a a b
a b b
b b b
a a a
b
b b
b b b
*/
/* META TAGS:
ALGO: high
BETA: no
DATE: 2024-11-19 23:30
DESC: see function name
PROC: no
TAGS: hierarchy
*/
for ( $j=0; $j<count( $t[0] ); $j++ ) { // each cell of line 0
$r[0][$j] = $t[0][$j]; // result table
} // for
for ( $i=1; $i<count( $t ); $i++ ) { // each line from line 1 on
for ( $j=0; $j<count( $t[$i] ); $j++ ) { // each cell
$sw = 0; // switch record mode off
for ( $k=0; $k<=$j; $k++ ) { // each cell until/including cell $j
if ( $t[$i][$k] != $t[$i-1][$k] ) $sw = 1; // compare with cell above; any mismatch puts switch record mode on
} // for
if ( $sw == 1 ) $cr = $t[$i][$j]; // $cr =^= result table cell
else $cr = "";
$r[$i][$j] = $cr;
} // for
} // for
return $r;
} // function
function get_non_hierarchical_table_from_hierarchical_table( $t ) {
/* USE:
$t = array(
array( 'a', 'a', 'a' ),
array( '', '', 'b' ),
array( '', 'b', 'b' ),
array( 'b', 'b', 'b' )
);
$r = get_non_hierarchical_table_from_hierarchical_table( $t );
*/
/* RESULT:
$r = array(
array( 'a', 'a', 'a' ),
array( 'a', 'a', 'b' ),
array( 'a', 'b', 'b' ),
array( 'b', 'b', 'b' )
);
*/
/* VIEW:
a a a
b
b b
b b b
a a a
a a b
a b b
b b b
*/
/* META TAGS:
ALGO: medium
BETA: no
DATE: 2024-11-19 23:45
DESC: see function name
PROC: no
TAGS: hierarchy
*/
for ( $j=0; $j<count( $t[0] ); $j++ ) { // each cell of line 0
$r[0][$j] = $t[0][$j]; // result table
} // for
for ( $i=1; $i<count( $t ); $i++ ) { // each line, starting with line 1 (not 0)
for ( $j=0; $j<count( $t[$i] ); $j++ ) { // each cell
$ct = $t[$i][$j]; // this cell
$cp = $t[$i-1][$j]; // prev. cell
if ( $ct == "" ) $cr = $cp; // if cell is empty: take cell above // $cr =^= cell result
else $cr = $ct; // else: take this cell
$r[$i][$j] = $cr;
} // for
} // for
return $r;
} // function
// hierarchy / hierarchical table <=> hierarchical data array
function get_hierarchical_data_array_from_hierarchical_table( $t ) {
/* VIEW:
a a a
b
b b
b b b
a 1
a 2
a 3
b 3
b 2
b 3
...
*/
$c = 0;
for ( $i=0; $i<count( $t ); $i++ ) { // each line
for ( $j=0; $j<count( $t[$i] ); $j++ ) { // each cell
$ct = $t[$i][$j]; // this cell
if ( $ct != "" ) $r[$c++] = array(
$ct,
$j+1
);
} // for
} // for
return $r;
} // function
function get_hierarchical_table_from_hierarchical_data_array( $a ) {
/* VIEW:
a 1
a 2
a 3
b 3
b 2
b 3
...
a a a
b
b b
b b b
*/
$al = count( $a ); // array length
// get (max.) col.s length
$cl = 0; // col.s length (table)
for ( $i=0; $i<$al; $i++ ) { // each array el.
$p = $a[$i][1]; // position
if ( $p > $cl ) $cl = $p;
} // for
// get rows length
$rl = 0; // rows length
for ( $i=0; $i<$al; $i++ ) { // each array el.
$p = $a[$i][1]; // position
if ( $p == $cl ) $rl++;
} // for
// create empty table
for ( $i=0; $i<$rl; $i++ ) { // each array el.
for ( $j=0; $j<$cl; $j++ ) { // each cell;
$tr[$i][$j] = ""; // table reversed (rows)
} // for
} // for
// fill table with values, if possible; rows reversed
$rc = -1; // row counter
for ( $i=$al-1; $i>=0; $i-- ) { // each array el., in reversed way
$v = $a[$i][0]; // value
$p = $a[$i][1]; // position
if ( $p == $cl ) $rc++; // new row
$tr[$rc][$p-1] = $v; // table reversed (rows)
} // for
// reverse rows
$r = array_reverse( $tr );
// return
return $r;
} // function
?>