猿问

PHP - 使用 foreach 显示二维数组

我想显示一个二维数组。我尝试使用双 foreach 来实现它,但我有点迷失了。它返回我一个空的 td,我不知道为什么。我使用 ZendFramework。我应该使用其他解决方案并离开 foreach 解决方案还是我只是想念一些东西?


数组(一个数组中有 7 个数组,列始终相同):


array(7) {

[0]=>

array(11) {

["'01.drplapostej-1'"]=>

string(20) "01. DRP La poste J-1"

["import"]=>

string(1) "0"

["radm_ok"]=>

string(1) "0"

["radm_rejet"]=>

string(1) "0"

["radm_ko"]=>

string(1) "0"

["rpec_ok"]=>

string(1) "0"

["rpec_rejet"]=>

string(1) "0"

["rpec_ko"]=>

string(1) "0"

["factu_ok"]=>

string(1) "0"

["factu_rejet"]=>

string(1) "0"

["factu_ko"]=>

string(1) "0"

}

[1]=>

array(11) {

["'01.drplapostej-1'"]=>

string(20) "02. DRL La poste J-1"

["import"]=>

string(2) "80"

["radm_ok"]=>

string(1) "0"

["radm_rejet"]=>

string(1) "0"

["radm_ko"]=>

string(1) "0"

["rpec_ok"]=>

string(1) "0"

["rpec_rejet"]=>

string(1) "0"

["rpec_ko"]=>

string(1) "0"

["factu_ok"]=>

string(1) "0"

["factu_rejet"]=>

string(1) "0"

["factu_ko"]=>

string(1) "0"

}

我试图做的是:


<table>

<tr>

<th>01.DRPLAPOSTEJ-1</th>

<th>IMPORT</th> 

<th>RADM_OK</th>

<th>RADM_REJET</th>

<th>RADM_KO</th>

<th>RPEC_OK</th>

<th>RPEC_REJET</th>

<th>RPEC_KO</th>

<th>FACTU_OK</th>

<th>FACTU_REJET</th>

<th>FACTU_KO</th>

</tr>

<?php foreach ($this->suiviprodjours as $value): ?>

  <?php foreach ($value as $key => $val): ?>

<tr>

<td <?php echo $val[$key]["01.DRPLAPOSTEJ-1"].'<br>';  ?></td>

</tr>

<?php endforeach; ?>

<?php endforeach; ?>

</table>

结果 :

有只小跳蛙
浏览 171回答 2
2回答

白衣非少年

您需要foreach()像下面这样申请:-<?php foreach($array as $arr){?>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <?php foreach($arr as $value){?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><?php echo $value;?></td>&nbsp; &nbsp; &nbsp; &nbsp; <?php } ?>&nbsp; &nbsp; </tr><?php } ?>所以完整的代码将是:<table>&nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; <th>01.DRPLAPOSTEJ-1</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>IMPORT</th>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; <th>RADM_OK</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>RADM_REJET</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>RADM_KO</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>RPEC_OK</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>RPEC_REJET</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>RPEC_KO</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>FACTU_OK</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>FACTU_REJET</th>&nbsp; &nbsp; &nbsp; &nbsp; <th>FACTU_KO</th>&nbsp; &nbsp; </tr>&nbsp; &nbsp; <?php foreach($array as $arr){?>&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php foreach($arr as $value){?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><?php echo $value;?></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <?php } ?>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; <?php } ?></table>

慕尼黑的夜晚无繁华

您可以将此代码段用于动态标题和数据,echo "<table>";$keys = array_keys($arr[0]);//if you want to make upper cases$keys = array_map("strtoupper", $keys);echo "<tr><th>";echo implode("</th><th>", $keys);echo "</th></tr>";foreach ($arr as $key => $value) {&nbsp; &nbsp; echo "<tr><td>";&nbsp; &nbsp; echo implode("</td><td>", $value);&nbsp; &nbsp; echo "</td></tr>";}echo "</table>";
随时随地看视频慕课网APP
我要回答