PHP 动态 Table 数据结果垂直而非水平

PHP Dynamic Table Data Results Vertically not Horizontally

我有一组数据,希望以动态方式显示 table 但需要垂直显示。

目前我得到以下输出:

示例 1:

1 - 2 - 3 - 4
5 - 6 - 7 - 8
9 - ... etc.

但我需要它:

示例 2:

1 - 4 - 7
2 - 5 - 8
3 - 6 - 9

我搜索了 Google 和堆栈,但找不到可用于解决我的问题的直接答案。

$array = array("4|Four","12|Twelve","2|Two","5|Five","11|Eleven","3|Three","1|One","6|Six","10|Ten","8|Eight","7|Seven","9|Nine");

$maxcols = 3;
$i = 0;

$table = "<table width=\"80%\" border=\"0\">\n";
$table .= "<tr>\n";


if(!empty($array))
{
rsort($array,SORT_NUMERIC);
for($p=0;$p<sizeof($array);$p++)
{
list($num,$title) = explode("|", trim($array[$p]));


if ($i == $maxcols) 
{
$i = 0;
$table .= "</tr>\n<tr>\n";
}
$table .= "<td>"."[ ".$num." ]"." ".$title."</td>\n";
$i++;
}

while ($i < $maxcols) 
{
$table .= "<td>&nbsp;</td>\n";
$i++;
}

$table .= "</tr>\n";
$table .= "</table>\n";
}

echo $table;

上面的代码输出如第一个示例中所示,但我无法理解如何使其输出如第二个示例中所示。

我就是这样做的。

    <table align="center" style="width:40%;">
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
</tr>
<?
$rotate = 0;
$amount = 1;
while($amount <= 50)
{
if($rotate == 0) {
echo '<tr>
<td>test</td>';
$rotate = 1;
}
else if($rotate == 1) {
echo '<td>test2</td>';
$rotate = 2;
}
else if($rotate == 2) {
echo '<td>test3</td>';
$rotate = 0;
echo '</tr>';
}

$amount++;

}
echo '</tr></table>';

这将在空白 php 页面上工作 - 因此您可以看到它是如何工作的。

我最初想使用 array_chunk 将数组分成多行,但后来我想到只用数学也可以做到。

$rows = 3; // define how many rows as you want

$array = array("4|Four","12|Twelve","2|Two","5|Five","11|Eleven","3|Three","1|One",
    "6|Six","10|Ten","8|Eight","7|Seven","9|Nine");
rsort($array,SORT_NUMERIC);

// determine number of columns needed to fit the values into the number of rows you want
$columns = count($array) / $rows; 

$table = '<table width="80%" border="0">';
for ($i=0; $i < $rows; $i++) { 
    $table .= '<tr>';
    for ($j=0; $j < $columns; $j++) {
        $index = (int) ($j * $rows + $i); // Do the math to get the proper array index
        if (isset($array[$index])) {                    
            list($num,$title) = explode("|", trim($array[$index]));
            $table .= "<td>"."[ ".$num." ]"." ".$title."</td>\n";
        } else {
            $table .= '<td>&nbsp;</td>';
        }
    }
    $table .= '</tr>';
}
$table .= '</table>';

上面的示例生成这样的网格,其中数字是数组键。

0  3  6  9
1  4  7  10
2  5  8  11

用两个 for 循环生成此网格的数学运算如下:

0*3+0  1*3+0  2*3+0  3*3+0
0*3+1  1*3+1  2*3+1  3*3+1
0*3+2  1*3+2  2*3+2  3*3+2

或者换句话说,(列索引 * 总行数)+ 行索引。无论您要指定行数还是列数,嵌套循环部分都将保持不变。唯一需要更改的是开始时的数学计算,以计算指定数量的列需要多少行,反之亦然。如果将开始部分更改为

$columns = 3; 
$rows = ceil(count($array) / $columns);

那么您应该能够指定列数而不是行数。 ceil 是必需的,因为除法的任何剩余部分都必须四舍五入才能获得所需的总行数。我认为这就是为什么您在第一次切换行和列时得到重复值的原因。

为了进一步扩展@DontPanic 的回答,可以将转换数组键的问题抽象为您可以拥有任意数量的列。两种实现的数学原理基本相同。

// returns a closure that can be used to translate the index order

function getIndexTranslator($list, $numColumns) {
    $numRows = ceil(count($list)/$numColumns);
    return function($x) use ($list, $numColumns, $numRows){
        return ($x % $numColumns) * $numRows + floor($x / $numColumns);
    };
}

然后您可以使用返回的函数让您按照所需的文档顺序遍历元素。以下是您将如何使用上述功能。

$transformer = getIndexTranslator($arr, 3);

for($x = 0; $x < count($arr); $x++){
    echo $arr[$transformer($x)], $x % 3 == 2?"\n":" ";
}

自己动手玩一下 here