A simple Google search will reveal a multitude of solutions for converting a two dimension array into HTML using PHP. Unfortunately none of these has the answers I am looking for.
一个简单的谷歌搜索将揭示使用PHP将二维数组转换为HTML的众多解决方案。不幸的是,这些都没有我想要的答案。
I want a generic piece of code that converts an array into a HTML table. Where most examples go wrong is that they assume the programmer knows the name of the table's fields . I want this code to be generic such that I can use it even if I do not know the name of the fields.
我想要一个将数组转换为HTML表的通用代码。大多数示例出错的地方在于它们假设程序员知道表字段的名称。我希望这段代码是通用的,即使我不知道字段的名称,我也可以使用它。
I can see I need two loops. One nested inside of the other. What I am not sure of is how to get the values out given I don't know the keys.
我可以看到我需要两个循环。一个嵌套在另一个里面。由于我不知道密钥,我不确定如何获取值。
The end result will hopefully output html something like this:
最终的结果有望输出这样的html:
<th>
<td>x/y</td>
<td> x1 </td>
<td> x2 </td>
</th>
<tr>
<td>y1</td>
<td> x1y1 </td>
<td> x2y1 </td>
</tr>
<tr>
<td>y2</td>
<td> x1y2 </td>
<td> x2y2 </td>
</tr>
Please remember I want a generic and simple solution. I hope this is clear.
请记住,我想要一个通用而简单的解决方案。我希望这很清楚。
3 个解决方案
#1
13
The following code will look through the two dimensions of the array and make them into a table. Regardless of what the key
may be, you will get a visual representation of it. If they do have key name and not just an index, the values will be available in $key
and $subkey
respectively. So you have them if you need them.
以下代码将查看数组的两个维度,并将它们组成一个表。无论密钥是什么,您都可以直观地看到它。如果它们确实具有键名而不仅仅是索引,则这些值将分别在$ key和$ subkey中可用。如果你需要它们,你就拥有它们。
The code:
代码:
$myarray = array("key1"=>array(1,2,3,4),
"key2"=>array(2,3,4,5),
"key3"=>array(3,4,5,6),
"key4"=>array(4,5,6,7)); //Having a key or not doesn't break it
$out = "";
$out .= "<table>";
foreach($myarray as $key => $element){
$out .= "<tr>";
foreach($element as $subkey => $subelement){
$out .= "<td>$subelement</td>";
}
$out .= "</tr>";
}
$out .= "</table>";
echo $out;
The result:
结果:
If you want to see the keys as headings, you could add this code after the echo "<table>";
line:
如果要将键看作标题,可以在echo“
echo "<tr>";
foreach($myarray as $key => $element) echo "<td>$key</td>";
echo "</tr>";
Resulting in this:
结果如下:
#2
1
You'll need two loops. One to loop through the first level, and another to go through the second level. This assumes that your two dimensional array is regularly rectangular.
你需要两个循环。一个循环通过第一级,另一个循环通过第二级。这假设您的二维数组是规则矩形。
//our example array
$foo['one']['a'] = '1a';
$foo['one']['b'] = '1b';
$foo['two']['a'] = '2a';
$foo['two']['b'] = '1b';
//open table
echo '<table>';
//our control variable
$first = true;
foreach($foo as $key1 => $val1) {
//if first time through, we need a header row
if($first){
echo '<tr><th></th>';
foreach($val1 as $key2 => $value2){
echo '<th>'.$key2.'</th>';
}
echo '</tr>';
//set control to false
$first = false;
}
//echo out each object in the table
echo '<tr><td>'.$key1.'</td>';
foreach($val1 as $key2 => $value2){
echo '<td>'.$value2.'</td>';
}
echo '</tr>';
}
echo '</table>';
Haven't tested it, but that should do it for you. First level of the array is rows, second level of the array is columns.
没有测试过,但那应该为你做。数组的第一级是行,第二级数组是列。
Sample Output for our $foo array:
$ foo数组的示例输出:
+-----+-----+-----+
| | a | b |
+-----+-----+-----+
| one | 1a | 1b |
+-----+-----+-----+
| two | 2a | 2b |
+-----+-----+-----+
#3
-1
Your loops will be something like this:
你的循环将是这样的:
foreach($myArray as $k => $v)
In $k you'll kave the key, in $v the value... Then you can print both.
在$ k中你会输入密钥,以$ v为单位...然后你可以打印两者。
#1
13
The following code will look through the two dimensions of the array and make them into a table. Regardless of what the key
may be, you will get a visual representation of it. If they do have key name and not just an index, the values will be available in $key
and $subkey
respectively. So you have them if you need them.
以下代码将查看数组的两个维度,并将它们组成一个表。无论密钥是什么,您都可以直观地看到它。如果它们确实具有键名而不仅仅是索引,则这些值将分别在$ key和$ subkey中可用。如果你需要它们,你就拥有它们。
The code:
代码:
$myarray = array("key1"=>array(1,2,3,4),
"key2"=>array(2,3,4,5),
"key3"=>array(3,4,5,6),
"key4"=>array(4,5,6,7)); //Having a key or not doesn't break it
$out = "";
$out .= "<table>";
foreach($myarray as $key => $element){
$out .= "<tr>";
foreach($element as $subkey => $subelement){
$out .= "<td>$subelement</td>";
}
$out .= "</tr>";
}
$out .= "</table>";
echo $out;
The result:
结果:
If you want to see the keys as headings, you could add this code after the echo "<table>";
line:
如果要将键看作标题,可以在echo“
echo "<tr>";
foreach($myarray as $key => $element) echo "<td>$key</td>";
echo "</tr>";
Resulting in this:
结果如下:
#2
1
You'll need two loops. One to loop through the first level, and another to go through the second level. This assumes that your two dimensional array is regularly rectangular.
你需要两个循环。一个循环通过第一级,另一个循环通过第二级。这假设您的二维数组是规则矩形。
//our example array
$foo['one']['a'] = '1a';
$foo['one']['b'] = '1b';
$foo['two']['a'] = '2a';
$foo['two']['b'] = '1b';
//open table
echo '<table>';
//our control variable
$first = true;
foreach($foo as $key1 => $val1) {
//if first time through, we need a header row
if($first){
echo '<tr><th></th>';
foreach($val1 as $key2 => $value2){
echo '<th>'.$key2.'</th>';
}
echo '</tr>';
//set control to false
$first = false;
}
//echo out each object in the table
echo '<tr><td>'.$key1.'</td>';
foreach($val1 as $key2 => $value2){
echo '<td>'.$value2.'</td>';
}
echo '</tr>';
}
echo '</table>';
Haven't tested it, but that should do it for you. First level of the array is rows, second level of the array is columns.
没有测试过,但那应该为你做。数组的第一级是行,第二级数组是列。
Sample Output for our $foo array:
$ foo数组的示例输出:
+-----+-----+-----+
| | a | b |
+-----+-----+-----+
| one | 1a | 1b |
+-----+-----+-----+
| two | 2a | 2b |
+-----+-----+-----+
#3
-1
Your loops will be something like this:
你的循环将是这样的:
foreach($myArray as $k => $v)
In $k you'll kave the key, in $v the value... Then you can print both.
在$ k中你会输入密钥,以$ v为单位...然后你可以打印两者。