php - 如何在html表格中垂直显示两列的两个不同的多维数组

标签 php mysql html

我的 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/

相关文章:

mysql - MySQL 插入期间清理

html - 导航菜单的移位元素

html - 我可以在全局 box-sizing :border-box in my project? 中使用吗

php - 搜索表 MySQL 和 PHP

mysql - sql 请求最大分组依据

javascript - Bootstrap 模式数据远程在 Chrome 中不起作用

php - 如何从本地mysql更新数据到远程mysql服务器?

php - 在 MySQL 中使用 IN/LIKE、TRIM 或 CONTAINS 提取数据库中的变量数据

php - 带有子查询的 Doctrine Update 查询

php - 将 javascript 值分配给 php 变量