Php 在保持表格结构的同时垂直显示表格值

Php 在保持表格结构的同时垂直显示表格值,php,Php,我们正在研究用于在表格中垂直排列数组值的其他方法,但大多数方法相当于将表格向右翻转90度。我一直在想一个方法来正确地实现这一点,但我想我需要一些帮助 例如,表格(水平顺序): 重新排序为(垂直顺序): 如您所见,由于最后2个单元格为空,因此结构保留 不是这样的: a e i m q b f j n r c g k o d h l p 在本例中,表格类似于将其侧向翻转。有人知道如何正确地做到这一点吗?编辑:这比我想象的要难,我第一次(或两次)就把它搞砸了。现在应该可以

我们正在研究用于在表格中垂直排列数组值的其他方法,但大多数方法相当于将表格向右翻转90度。我一直在想一个方法来正确地实现这一点,但我想我需要一些帮助

例如,表格(水平顺序):

重新排序为(垂直顺序):

如您所见,由于最后2个单元格为空,因此结构保留

不是这样的:

a  e  i  m  q
b  f  j  n  r
c  g  k  o
d  h  l  p

在本例中,表格类似于将其侧向翻转。有人知道如何正确地做到这一点吗?

编辑:这比我想象的要难,我第一次(或两次)就把它搞砸了。现在应该可以了

假设您的表结构存储在二维数组中:

$data = array(
  array('a', 'b', 'c', 'd', 'e'),
  array('f', 'g', 'h', 'i', 'j'),
  array('k', 'l', 'm', 'n', 'o'),
  array('p', 'q', 'r')
);
由于要保持相同的“形状”,需要确定表的尺寸。为此,我们可以获取第一行的
计数
,因为我们知道第一行必须是表的最大宽度。高度就是数组中的元素数

$width = count($data[0]); // 5
$height = count($data);   // 4
我们还需要元素的总数,但如果采用$width*$height,我们可能会高估

$total = $width * $height; // 20
然后,它实际上只是一个小数学计算的东西去。我们必须为旧指数和新指数使用单独的计数器,因为一旦出现漏洞,我们必须以不同的方式增加它们

$new_data = array();
$j = 0;
for($i = 0; $i < $total; $i++) {
  $old_x = floor($i / $width); // integer division
  $old_y = $i % $width;        // modulo

  do {
    $new_x = $j % $height;        // modulo
    $new_y = floor($j / $height); // integer division
    $j++;
  // move on to the next position if we have reached an index that isn't available in the old data structure
  } while (!isset($data[$new_x][$new_y]) && $j < $total);

  if (!isset($new_data[$new_x])) {
    $new_data[$new_x] = array();
  }
  if (isset($data[$old_x][$old_y])) {
    $new_data[$new_x][$new_y] = $data[$old_x][$old_y];
  }
}
$new_data=array();
$j=0;
对于($i=0;$i<$total;$i++){
$old_x=地板($i/$width);//整数除法
$old_y=$i%$width;//模
做{
$new_x=$j%$height;//模
$new_y=floor($j/$height);//整数除法
$j++;
//如果我们达到了旧数据结构中不可用的索引,请转到下一个位置
}而(!isset($data[$new_x][$new_y])&&$j<$total);
如果(!isset($new_data[$new_x])){
$new_data[$new_x]=数组();
}
如果(isset($data[$old\u x][$old\u y])){
$new_data[$new_x][$new_y]=$data[$old_x][$old_y];
}
}

诀窍是为“挂起”列减去一。即最后一行中缺少值的列

// setup some initial test data... rip this out and replace with whatever
$values = array();
for ($x = 0; $x < 18; ++$x)
  $values[] = chr($x + ord("a"));
// the number of columns that will "hang" or miss a value in the last row
$hanging_columns = $columns * $rows - count($values);

$counter = 0;
for ($y = 0; $y < $rows; ++$y) {
  for ($x = 0; $x < $columns; ++$x) {
    // if we've displayed all values, stop
    if ($counter++ >= count($values)) break;

    // calculate the correct index to display
    $index = ($y + $x * $rows);
    // if we are in a hanging column, we need to back up by one
    if ($x > $columns - $hanging_columns) $index -= 1;

    // display the value
    echo $values[$index] . " ";
  }
  echo "\n";
}
有些柱子悬挂着。即该列在最后一行中缺少值

// setup some initial test data... rip this out and replace with whatever
$values = array();
for ($x = 0; $x < 18; ++$x)
  $values[] = chr($x + ord("a"));
// the number of columns that will "hang" or miss a value in the last row
$hanging_columns = $columns * $rows - count($values);

$counter = 0;
for ($y = 0; $y < $rows; ++$y) {
  for ($x = 0; $x < $columns; ++$x) {
    // if we've displayed all values, stop
    if ($counter++ >= count($values)) break;

    // calculate the correct index to display
    $index = ($y + $x * $rows);
    // if we are in a hanging column, we need to back up by one
    if ($x > $columns - $hanging_columns) $index -= 1;

    // display the value
    echo $values[$index] . " ";
  }
  echo "\n";
}
//将“挂起”或丢失最后一行中的值的列数
$hanging_columns=$columns*$rows-count($values);
$counter=0;
对于($y=0;$y<$rows;++$y){
对于($x=0;$x<$columns;++$x){
//如果已显示所有值,请停止
如果($counter++>=count($values))中断;
//计算要显示的正确索引
$index=($y+$x*$行);
//如果我们处于悬而未决的状态,我们需要后退一步
如果($x>$columns-$hanging\u columns)$index-=1;
//显示值
回显$value[$index]。“”;
}
回音“\n”;
}
这里是另一个解决方案

$unflipped = array('a', 'b', 'c', 'd', 'e', 
               'f', 'g', 'h', 'i', 'j',
               'k', 'l', 'm', 'n');


function flip90($arr, $lineHeight) 
{
    $tbl = array();

    $index = 0;
    $counter = 1;


    for($i = 0; $i < count($arr); $i++)
    {
        if($counter > $lineHeight)
        {
            $index++;
            $counter = 1;
        }

        if($counter <= $lineHeight) 
        {
            $tbl[$index][$counter] = $arr[$i];

            $counter++;
        }

    }

    return $tbl;
}

$flipped = flip90($unflipped, 5);
echo "<pre>";
var_dump($flipped);
echo "<pre>";
$unflipped=array('a','b','c','d','e',
‘f’、‘g’、‘h’、‘i’、‘j’,
‘k’、‘l’、‘m’、‘n’;
功能flip90($arr,$lineHeight)
{
$tbl=数组();
$index=0;
$counter=1;
对于($i=0;$i$lineHeight)
{
$index++;
$counter=1;
}

如果($counter)您想从数据库中呈现表值吗?是的,但您可以保持简单,并在本例中使用数组:
array('a','b','c','d','e'…)
.Oops,在我第一次发布之前我没有测试它,我改变了宽度和高度。帖子现在是固定的。我尝试了,但它不起作用。你的第一行显示为
a e I m q
,而它应该是
a e I m p
。因为第一行是错误的,那么其余的行也会移动。另外,第1-3行也需要移动有5列,而在你的示例中只有第1-2行有5个元素。好吧,我考虑了更多,并更新了答案。现在应该可以了。答案仍然是
O(n^2)
,因为你正在循环遍历总宽度*高度抱歉,我的数学一直不好,但是
O(n^2)中的
O
是什么
它如何适合此解决方案?
$unflipped = array('a', 'b', 'c', 'd', 'e', 
               'f', 'g', 'h', 'i', 'j',
               'k', 'l', 'm', 'n');


function flip90($arr, $lineHeight) 
{
    $tbl = array();

    $index = 0;
    $counter = 1;


    for($i = 0; $i < count($arr); $i++)
    {
        if($counter > $lineHeight)
        {
            $index++;
            $counter = 1;
        }

        if($counter <= $lineHeight) 
        {
            $tbl[$index][$counter] = $arr[$i];

            $counter++;
        }

    }

    return $tbl;
}

$flipped = flip90($unflipped, 5);
echo "<pre>";
var_dump($flipped);
echo "<pre>";