我有一个从文件结构中提取的多维数组。
我正在尝试使用此数组根据每个项目的键创建动态HTML表。 我用来拉取文件结构的脚本是将每个文件夹放入数组的键中。
所以数组输出如下:
Array(
[english] => Array
(
[term1] => Array
(
[circ] => Array
(
[Unit1] => Array
(
[0] => file.zip
)
[Unit2] => Array
(
[0] => file.zip
)
[Unit3] => Array
(
[0] => file.zip
)
[Unit4] => Array
(
[0] => file.zip
)
)
[type] => Array
(
[Unit1] => Array
(
[0] => file.zip
)
[Unit2] => Array
(
[0] => file.zip
)
[Unit3] => Array
(
[0] => file.zip
)
[Unit4] => Array
(
[0] => file.zip
)
)
)
[term2] => Array
(
[circ] => Array
(
[Unit1] => Array
(
[0] => file.zip
)
[Unit2] => Array
(
[0] => file.zip
)
[Unit3] => Array
(
[0] => file.zip
)
[Unit4] => Array
(
[0] => file.zip
)
)
[type] => Array
(
[Unit1] => Array
(
[0] => file.zip
)
[Unit2] => Array
(
[0] => file.zip
)
[Unit3] => Array
(
[0] => file.zip
)
[Unit4] => Array
(
[0] => file.zip
)
)
)
)
)
我想要输出的是一个带有表的页面,该表将使用这样的键。
title = engligh
标题1 = term1
标题2 = circ
内容1 =
内容2 =单元1
内容2 =单元2
link = file.zip
{title}
<table class="table table-hover">
<thead>
<th>{heading1}</th>
<th>{heading2}</th>
</thead>
<tbody>
<tr>
<td>{content1}</td>
<td><a href="{link}">{content2}</a></td>
</tr>
</tbody>
</table>
我在5年多的时间里没有使用过PHP,说实话从来没有在这方面做过很好的事情,我只是在工作中被抛到深处,需要一些帮助来实现这一点所以给予任何帮助非常感谢!
修改
所以我创建了一个从数组中拉出前2级键的函数,但是当我添加到第三级时,它只是重复第二级。 我认为递归更符合我的需要,但我无法理解这一点。
function recursive(array $array){
foreach($array as $key => $value){
echo $key, '<br>';
//If $value is an array.
if(is_array($value)){
//We need to loop through it.
foreach($value as $key1 => $value1) {
echo ' - ' . $key1, '<br>';
//if $value is an array.
if(is_array($value1)){
foreach ($value1 as $key2 => $value2) {
echo ' - ' . $key2, '<br>';
}
}else{
echo ' - ' . $key1, '<br>';
}
}
} else{
//It is not an array, so print it out.
echo $key, '<br>';
}
}
}
任何人都可以告诉我在这个功能中获得如此多级别的错误吗?
答案 0 :(得分:0)
我不确定你想要怎么打印。示例结果有点令人困惑。这应该会告诉你如何处理数组。我会看看我是否可以让它更接近你在帖子中所拥有的内容但是我没有得到最低级别信息应该完全正确的地方......
print "<style>table, td, tr {border: 1px solid black;}</style>";
print "<table>";
foreach($array as $class => $c_arr) {
foreach($c_arr as $term => $t_arr) {
foreach($t_arr as $circ_term => $ct_arr) {
print "<tr><td>$class</td>";
print "<td>$term</td>";
print "<td>$circ_term</td>";
foreach($ct_arr as $unit => $u_val) {
print "<td>$unit = {$u_val[0]}</td>";
}
print "</tr>";
}
}
}
print "</table>";
title = engligh
heading 1 = term1
heading 2 = circ
content 1 =
content 2 = Unit1
content 2 = Unit2
link = file.zip
所以...键入会发生什么?每次都列出4个file.zip。为什么内容1空白?没有线索。你也可以尝试这个:
foreach($array as $class => $c_arr) {
foreach($c_arr as $term => $t_arr) {
print "title = $class<br/>";
print "heading 1 = $term<br/>";
foreach($t_arr as $circ_term => $ct_arr) {
print "heading 2 = $circ_term<br/>";
foreach($ct_arr as $unit => $u_val) {
print "$unit = {$u_val[0]}<br/>";
}
}
print "<br/><br/>";
}
print "<br/><br/>";
}
/*
title = english
heading 1 = term1
heading 2 = circ
Unit1 = file.zip
Unit2 = file.zip
Unit3 = file.zip
Unit4 = file.zip
heading 2 = type
Unit1 = file.zip
Unit2 = file.zip
Unit3 = file.zip
Unit4 = file.zip
title = english
heading 1 = term2
heading 2 = circ
Unit1 = file.zip
Unit2 = file.zip
Unit3 = file.zip
Unit4 = file.zip
heading 2 = type
Unit1 = file.zip
Unit2 = file.zip
Unit3 = file.zip
Unit4 = file.zip
*/