我的 php 代码抛出值,但将两个数组数据混合在一起。如何修复代码来整理它!提前致谢!
此处 $Array1=Array ( [0] => Array ( [0] => Abdul Razak, Ahmed Deedat [name] => Abdul Razak, Ahmed Deedat [1] => 11 [标准] => 11 [2 ] => 12/08/16 [日期] => 12/08/16)) $Array2=数组 ( [0] => 数组 ( [0] => lali, raj [名称] => lalit, raj [1] => 12 [标准] => 12 [2] => 12/08/16 [日期] => 16/08/12))
以上数组只是不完整的示例,行数超过 10 我想要这样的输出:
left wing | right wing
array 1, key [0] values | array 2 [0] key values
array1, key [1] values | array 2 [1] key values
and so on till both arrays doesn't end
代码如下所示:
<?php
<table id="fire">
<caption><?php echo "Achievements by the selected two are:";?> </caption>
<tr>
<th>Wing 1</th>
<th>Wing 2</th>
</tr>
<tr>
<?php
if(isset($raws1)) {
foreach($raws1 as $raw1){ ?>
<tr>
<?php
if($raw1==null){
echo "<td>---</td>";
} else {
echo "<td>". $raw1['name'] . ", " . $raw1['standard'] .", on ".$raw1['dated']." , ".$raw1['achieved'].", at ".$raw1['at']. ", ".$raw1['on'].", (". $raw1['position'] ."</td>";
}
if(isset($raws2)) {
foreach($raws2 as $raw2){ ?>
<tr>
<?php
if($raw2==null){
echo "<td>---</td>";
} else {
echo "<td>". $raw2['name'] . ", " . $raw2['standard'] .", on ".$raw2['dated']." , ".$raw2['achieved'].", at ".$raw2['at']. ", ".$raw2['on'].", (". $raw2['position'] ."</td></tr>";
} }}}}?>
</table>
?>
最佳答案
输出带有标题的表格的开始部分看起来已经很好了。
<table id="fire">
<caption><?php echo "Achievements by the selected two are:";?> </caption>
<tr>
<th>Wing 1</th>
<th>Wing 2</th>
</tr>
<?php
这是执行 PHP 部分的一种方法。将两个数组放入一个数组中。这将有助于防止以后出现重复的代码,因为您可以对外部数组进行 foreach
对两个内部数组执行相同的操作。
$both = [$raw1, $raw2];
获取两个数组的最大计数。
$count = max(array_map('count', $both));
然后您可以使用 for
循环来循环到最大计数。
for ($i=0; $i < $count; $i++) {
echo '<tr>';
// This foreach loop will loop over the two arrays ($raw1 and $raw2)
foreach ($both as $array) {
// check the $i element of both the arrays and output its value if it's set
if (empty($array[$i])) {
echo "<td>---</td>";
} else {
$x = $array[$i];
echo "<td>$x[name], $x[standard], etc. ...";
}
}
echo '</tr>';
} ?>
</table>
关于php - 如何在html表格中垂直显示两列的两个不同的多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38903531/