php 将数组显示为表格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14047296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display Array as a Table
提问by
I am trying to print a table using PHP/HTML. Data stored inside array like this:
我正在尝试使用 PHP/HTML 打印表格。像这样存储在数组中的数据:
Array ( [id] => 1 [first_name] => mike [last_name] => lastname )
My code is as follow. It runs and there are no error however the output is not as expected. Here is the PHP/HTML code:
我的代码如下。它运行并且没有错误但是输出不符合预期。这是 PHP/HTML 代码:
<table>
<tr>
<th>1</th>
<th>2</th>
<th>3</th>
</tr>
<?php foreach ($res as $item): ?>
<tr>
<td><?php echo $item['id'] ?></td>
<td><?php echo $item['first_name'] ?></td>
<td><?php echo $item['last_name'] ?></td>
</tr>
<?php endforeach; ?>
</table>
The result I get is the first character of the items:
我得到的结果是项目的第一个字符:
1 2 3
1 1 1
m m m
l l l
Not sure what I am doing wrong? I would really appreciate an explanation.
不确定我做错了什么?我真的很感激解释。
UPDATE:
更新:
PHP CODE that has no errors:
没有错误的 PHP 代码:
<?php
foreach ($result as $row)
{
echo '<tr>';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['first_name'] . '</td>';
echo '<td>' . $row['last_name'] . '</td>';
echo '</tr>';
}
?>
And this is my array with only one "row" in it:
这是我的数组,其中只有一个“行”:
Output of $result variable using print_r($result) wrapped with < PRE > tags
使用 <PRE> 标签包裹的 print_r($result) 输出 $result 变量
Array
(
[id] => 3
[first_name] => Jim
[last_name] => Dude
)
Here is the result I am getting:
这是我得到的结果:
Actual Table result:
实际表结果:
ID First Name Last Name
3 3 3
J J J
D D D
However if I have 0 in array or more than 1 (meaning 2 or more) it works perfectly. IT JUST DOES NOT WORK WHEN I HAVE ONLY ONE "ROW" OF ELEMENTS INSIDE ARRAY. For example this array, works perfectly:
但是,如果我在数组中有 0 个或超过 1 个(意味着 2 个或更多),它就可以完美地工作。当我在数组中只有一个“行”元素时,它就不起作用。例如这个数组,完美地工作:
Array
(
[0] => Array
(
[id] => 3
[first_name] => Jim
[last_name] => Dude
)
[1] => Array
(
[id] => 4
[first_name] => John
[last_name] => Dude2
)
)
I get this result:
我得到这个结果:
ID First Name Last Name
3 Jim Dude
4 John Dude2
I am not really sure what I am doing wrong. The idea is not knowing how many items is in a Table read it into $resultvariable and then using this variable print all elements inside HTML table. Table could contain O elements, 1 row of elements, 1 or more rows of elements.
我不确定我做错了什么。这个想法是不知道表中有多少项将其读入$result变量,然后使用此变量打印 HTML 表中的所有元素。表格可以包含 O 个元素、1 行元素、1 行或多行元素。
回答by David Stetler
Do not remove your foreach loop. Without seeing your code where you build your array, my guess would be you are doing this:
不要删除您的 foreach 循环。如果没有看到构建数组的代码,我猜你会这样做:
$res = array('id' => 1, 'first_name'=>'mike', 'last_name'=>'lastname');
when in fact what you probably want to be doing is this:
实际上,您可能想要做的是:
$res[] = array('id' => 1, 'first_name'=>'mike', 'last_name'=>'lastname');
the brackets [ ] add an array of all the values to your array. This way you can loop through each as a collection of values, not individual values.
括号 [] 将所有值的数组添加到数组中。通过这种方式,您可以将每个作为值的集合循环,而不是单个值。
回答by Jovan Perovic
You data structure is not matrix but an array:
您的数据结构不是矩阵而是数组:
Array ( [id] => 1 [first_name] => mike [last_name] => lastname )
Array ( [id] => 1 [first_name] => mike [last_name] => lastname )
You need something like this to do what you want:
你需要这样的东西来做你想做的事:
Array(
[0] => Array ( [id] => 1 [first_name] => mike [last_name] => lastname ),
[1] => Array ( [id] => 1 [first_name] => mike [last_name] => lastname )
........
);
Apart from that, code seems fine...
除此之外,代码似乎很好......
回答by phani
<? Php
echo "<table border="2">";
foreach($ array as $ values)
{
echo "<tr><td>";
echo "$ values";
echo "</td></tr>";
}
?>
I wrote this for printing single array values into a table
我写这个是为了将单个数组值打印到表中
回答by Aswin Saravna
If the array is of std object type, you can't use [], instead use -> to get specific entities
如果数组是 std 对象类型,则不能使用 [],而是使用 -> 获取特定实体
<!DOCTYPE html>
<html>
<body>
<table>
<tr>
<th>id</th>
<th>First name</th>
<th>Last name</th>
<th>email</th>
</tr>
<?php
foreach($data as $item) {?>
<tr>
<td><?php echo $item->id; ?></td>
<td><?php echo $item->first_name; ?></td>
<td><?php echo $item->last_name ;?></td>
<td><?php echo $item->email;?></td>
</tr>
<?php }?>
</table>
</body>
</html>
回答by Sean Limpens
This is how I solved a similar problem I had with my webpage. I wanted the page to display an array of all classes a specific teacher teaches using session variables. I hope that, even though the subject might not be relevant to you, the general gist will help people out.
这就是我解决网页上类似问题的方法。我希望页面显示特定教师使用会话变量教授的所有课程的数组。我希望,即使该主题可能与您无关,但总体要点会帮助人们解决问题。
<?php
echo "<table border='1' style='border-collapse:
collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='150' align='center'>Class</td>";
echo "</tr>";
foreach ($query_class_array as $row)
{
echo '<td width="150" align=center>' . $row['Class'] . '</td>';
echo '</tr>';
}
?>
回答by Phrank
This will display any multidimensional array in an more readable format than print_r... Could be upgraded with different colors to contiguous cell to scroll rapidly through array... the space where you do not see an array,s name (key) or value will tell you the key/value if you hoover it... not pleasant visually but effective.
这将以比 print_r 更易读的格式显示任何多维数组...可以用不同的颜色升级到连续的单元格以快速滚动数组...您看不到数组的空间,名称(键)或值如果您使用它,它会告诉您键/值......视觉上不愉快但有效。
function show_array_as_table($array,$key_color='blue',$value_color="black",$fontfamily="arial",$fontsize="12pt") {
echo "<table style='color:{$color}; font-family:{$fontfamily}; font-size:{$fontsize}'>";
foreach($array as $key => $value) {
echo "<tr>";
// key cell
echo "<td title=\"{$key}\" style='width:150pt; height:25pt; text-align:right; vertical-align:top; background-color:cccccc; color:{$key_color}'><b>{$key}</b></td>";
// value cell (will contain a table if the value is an array)
echo "<td style='width:300pt; height:25pt; text-align:justify; vertical-align:top; color:{$value_color}'>";
if (is_array($value)) {
show_array_as_table($value,$key_color,$color,$fontfamily,$fontsize);
} else {
echo "<i>{$value}</i>";
}
echo "</td>";
echo "</tr>";
}
echo "</table>";
}
回答by sam
This will also work:
这也将起作用:
echo "<table>";
foreach ($result as $key){
echo "
<tr>
<td>'".$key['id']."'</td>
<td>'".$key['first_name']."'</td>
<td>'".$key['last_name']."'</td>
</tr></table>
";
}

