这是关联数组
$newArr=Array (
[565] => Array (
[3] => Array (
[1] => Array (
[1] => 1
)
)
)
[67677] => Array (
[4] => Array (
[1] => Array (
[1] => 1
)
)
)
[767] => Array (
[6] => Array (
[1] => Array (
[1] => 1
)
)
)
[76767] => Array (
[7] => Array (
[1] => Array (
[1] => 1
)
)
)
[878] => Array (
[8] => Array (
[1] => Array (
[1] => 1
)
)
[127] => Array (
[1] => Array (
[1] => 1
)
)
)
[87] => Array (
[7] => Array (
[1] => Array (
[1] => 1
)
)
)
[xyz] => Array (
[1] => Array (
[2] => Array (
[2] => 2
)
)
)
[abc] => Array (
[1] => Array (
[2] => Array (
[1] => 1
)
)
[4] => Array (
[1] => Array (
[1] => 1
)
)
[5] => Array (
[1] => Array (
[1] => 1
)
)
)
)
我想在html表格中显示它。与此关联数组的第二列对应的索引将在1到5之间变化,如果与该第二列值对应的第3列不存在,则将其赋予NULL或零。
我尝试了以下代码,但只打印了与每个主要子数组的最后一行对应的值。 (例如 -
abc 1 0 0
2 0 0
3 0 0
4 0 0
5 1 1
这是我一直在努力的代码。
foreach($newArr as $key1=>$value1){
if(is_array($value1)){
foreach($value1 as $key2=>$value2){
if(is_array($value2)){
foreach($value2 as $key3=> $value3){
if(is_array($value3)){
foreach($value3 as $key4=> $value4){
}
}
}
}
echo "<td rowspan=5 >". $key1. "</td>";
for($i=1;$i<6;$i++){
if($key2==$i){
echo "<td>". $key2. "</td>";
echo "<td>". $key3. "</td>";
echo "<td>". $key4. "</td>";
} else{
echo "<td>". $i. "</td>";
echo "<td>". 0 . "</td>";
echo "<td>". 0 . "</td>";
}
echo "<tr>";
}
echo "</tr>";
}
}
}
我已经坚持了一段时间。如果有人能帮助我,我会感激不尽! 不要太苛刻我已经阅读了与foreach相对应的手册以及与此类似的其他问题,但我只是无法做到。 提前致谢!干杯!!
答案 0 :(得分:0)
那么问题是什么。问题是您正在进行foreach循环但不处理其中的任何代码。 $ key4将是unifined,因为它只能在foreach循环exept中使用,如果你创建它的全局。
foreach($newArr as $key1=>$value1){
if(is_array($value1)){
foreach($value1 as $key2=>$value2){
$var_key2 = $key2;
if(is_array($value2)){
foreach($value2 as $key3=> $value3){
$var_key3 = $key3;
if(is_array($value3)){
foreach($value3 as $key4=> $value4){
$var_key4 = $key4;
}
}
}
}
}
echo "<td rowspan=5 >". $key1. "</td>";
for($i=1;$i<6;$i++){
if($var_key2 == $i){
echo "<td>".$var_key2."</td>";
echo "<td>".$var_key3."</td>";
echo "<td>".$var_key4."</td>";
} else{
echo "<td>". $i. "</td>";
echo "<td>". 0 . "</td>";
echo "<td>". 0 . "</td>";
}
echo "<tr>";
}
$var_key2 = "";
$var_key3 = "";
$var_key4 = "";
echo "</tr>";
}
}