[英]Multidimensional associative array into html table
I'm currently learning php and I'm trying to print a multidimensional associative array in html table format and I'm having some trouble looping through each of the elements, here's the code我目前正在学习 php,我正在尝试以 html 表格格式打印多维关联数组,但在遍历每个元素时遇到了一些麻烦,这是代码
$test =array(
'One'=>array('fname' => 'John', 'lnom' => 'Dupond', 'age' => 25, 'city' => 'Paris'),
'Two' => array('fname' => 'Deal', 'lnom' => 'Martin', 'age' => 20, 'city' => 'Epizts'),
'Three' => array('fname' => 'Martin', 'lnom' => 'Tonge', 'age' => 18, 'city' => 'Epinay'),
'Four'=> array('fname' => 'Austin', 'lnom' => 'Dupond', 'age' => 33, 'city' => 'Paris'),
'Five'=> array('fname' => 'Johnny', 'lnom'=>'Ailta', 'age' => 46, 'city'=> 'Villetaneuse'),
'Six'=> array('fname' => 'Scott', 'lnom' => 'Askier', 'age'=>7, 'city'=>'Villetaneuse')
);
what I'm trying to do :我正在尝试做的事情:
foreach($test['One'] as $key=> $value)
{
echo $value;
}
I'm not too sure about the rest if I should use a nested foreach loop or something else to print all the keys + values ..如果我应该使用嵌套的 foreach 循环或其他东西来打印所有的键 + 值,我不太确定其余的..
If you want to loop through this array completely and turn it to an html table you need two loops - one for the main and second for inner array for example:如果要完全遍历此数组并将其转换为 html 表,则需要两个循环 - 一个用于主数组,第二个用于内部数组,例如:
<?php
$test =array(
'One'=>array('fname' => 'John', 'lnom' => 'Dupond', 'age' => 25, 'city' => 'Paris'),
'Two' => array('fname' => 'Deal', 'lnom' => 'Martin', 'age' => 20, 'city' => 'Epizts'),
'Three' => array('fname' => 'Martin', 'lnom' => 'Tonge', 'age' => 18, 'city' => 'Epinay'),
'Four'=> array('fname' => 'Austin', 'lnom' => 'Dupond', 'age' => 33, 'city' => 'Paris'),
'Five'=> array('fname' => 'Johnny', 'lnom'=>'Ailta', 'age' => 46, 'city'=> 'Villetaneuse'),
'Six'=> array('fname' => 'Scott', 'lnom' => 'Askier', 'age'=>7, 'city'=>'Villetaneuse')
);
?>
<table>
<tr>
<th>#</th>
<th>fname</th>
<th>lnom</th>
<th>age</th>
<th>city</th>
</tr>
<?php
foreach($test as $key => $val){
?><tr>
<td><?php echo $key;?></td><?php
foreach($val as $k => $v){
?><td><?php echo $v;?></td><?php
}
?></tr><?php
}
?>
</table>
Will return:将返回:
# fname lnom age city
One John Dupond 25 Paris
Two Deal Martin 20 Epizts
Three Martin Tonge 18 Epinay
Four Austin Dupond 33 Paris
Five Johnny Ailta 46 Villetaneuse
Six Scott Askier 7 Villetaneuse
Just loop the outer array:只需循环外部数组:
<table>
<?php foreach ($test as $person) { ?>
<tr>
<td><?= $person['fname'] ?></td>
<td><?= $person['lnom'] ?></td>
<td><?= $person['age'] ?></td>
<td><?= $person['city'] ?></td>
</tr>
<?php } ?>
</table>
The variable $person
will contain the sub array, from which you just fetch the elements you want.变量
$person
将包含子数组,您只需从中获取所需的元素。
If you want to output every column dynamically (not needing to write $person['fname']
etc), then you can create a nested loop:如果你想动态输出每一列(不需要写
$person['fname']
等),那么你可以创建一个嵌套循环:
<?php foreach ($test as $person) { ?>
<tr>
<?php foreach ($person as $value) { ?>
<td><?= $value ?></td>
<?php } ?>
</tr>
<?php } ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.