【问题标题】:PHP display associative array in HTML tablePHP在HTML表格中显示关联数组
【发布时间】:2011-10-13 22:47:27
【问题描述】:

这是我的关联数组:

$req_data1[]=array(
    'depart1'=>$_REQUEST['to'],
    'd_time1'=>$d_time5,
    'stop'=>"",
    'leave_stop'=>"",
    'arrival1'=>$_REQUEST['from'],
    'a_time1'=>$end_time5,
    'price1'=>intval($final_price),
    'air_line'=>"xxxxx");

这是我的排序算法:

foreach ($req_data as $key => $row) {
    $depart[$key]  = $row['depart'];
    $d_time[$key] = $row['d_time'];
    $stop[$key]  = $row['stop'];
    $leave_stop[$key] = $row['leave_stop'];
    $arrival[$key]  = $row['arrival'];
    $a_time[$key] = $row['a_time'];
    $price[$key] = $row['price'];
}

array_multisort($price,SORT_ASC, $req_data);

我正在显示排序后的数据:

foreach($req_data as $key=>$row) {
    echo "</br>";

    foreach($row as $key2=>$row2){
        echo $row2;
    }
}

我现在的问题是我想将该数组放入 HTML 表格中,但不知道如何操作。这是我到目前为止尝试过的代码,但它不起作用:

$cols = 5; 

echo "<table border=\"5\" cellpadding=\"10\">"; 

for ($r=0; $r < count($row2); $r++) { 
    echo "<tr>"; 

    for ($c=0; $c<$cols; $c++) { 

        ?> <td> <?php $input[$r+$c] ?> </td> <?php 
        }

    echo "</tr>"; 
    $r += $c; 
}

echo "</table>";
?>

谁能告诉我我的代码有什么问题,或者我如何将这些排序的数据显示到表格中?谢谢。

【问题讨论】:

  • 您能否格式化您的代码,使其更具可读性?这将使人们更容易为您提供帮助。

标签: php html associative-array


【解决方案1】:

为什么不直接修改你已经用来显示数据的循环呢?

echo "<table>";
foreach($req_data as $key=>$row) {
    echo "<tr>";
    foreach($row as $key2=>$row2){
        echo "<td>" . $row2 . "</td>";
    }
    echo "</tr>";
}
echo "</table>";

【讨论】:

    【解决方案2】:
    $toOutput = '<table>';
    $showHeader = true;
    $memberData = $reportObj->getMemberData();
    while($row = mysql_fetch_assoc($memberData))
    {
        $toOutput .= '<tr>';
    
        //Outputs a header if nessicary
        if($showHeader)
        {
            $keys = array_keys($row);
            for($i=0;$i<count($keys);$i++)
            {
                $toOutput .= '<td>' . $keys[$i] . '</td>';
            }
            $toOutput .= '</tr><tr>';
            $showHeader = false;
        }
    
        //Outputs the row
        $values = array_values($row);
        for($i=0;$i<count($values);$i++)
        {
            $toOutput .= '<td>' . $values[$i] . '</td>';
        }
    
        $toOutput .= '</tr>';
    }
    $toOutput .= '</table>';
    
    echo 'Test page';
    echo $toOutput;
    

    对死灵感到抱歉,但实际上我在写这篇文章时正在查看是否有内置函数。

    【讨论】:

    • 您是否真的在回答字段中提出问题?
    • 你找到内置函数了吗?
    • 不,我从来没有找到一个好的内置函数。我想这在 Laravel 等现代框架中得到了很好的处理。然而,我已经离开 PHP 开发。
    【解决方案3】:
         function DumpTable($array_assoc) {
            if (is_array($array_assoc)) {
                echo '<table class="table">';
                echo '<thead>';
                echo '<tr>';
                list($table_title) = $array_assoc;
                foreach ($table_title as $key => &$value):
                    echo '<th>' . $key . '</th>';
                endforeach;
                echo '</tr>';
                echo '</thead>';
                foreach ($array_assoc as &$master):
                    echo '<tr>';
                    foreach ($master as &$slave):
                        echo '<td>' . $slave . '</td>';
                    endforeach;
                    echo '</tr>';
                endforeach;
                echo '</table>';
                return;
            }
        }
    

    【讨论】:

    • 嘿,欢迎来到 StackOverflow!您能否解释一下您的代码以及它为何有效?
    【解决方案4】:
    echo "<table border=\"5\" cellpadding=\"10\">";
    for ($r=0; $r < count($row2); $r++) {
        echo "<tr>";
        for ($c=0; $c<$cols; $c++) { ?>
            <td> <?php $input[$r+$c] ?> </td>
        <?php }
    
        echo "</tr>";
        $r += $c;
    }
    echo "</table>";?>
    

    试试这样的

    echo "<table>";
    for($r=0;$r<count($row2);$r++){
    echo "<tr>";
    for($c=0;$c<$cols;$c++){
    echo "<td>".[VARIABLE YOU WANT TO PRINT]."</td>";
    }
    echo "</tr>";
    }
    echo "</table>";
    

    【讨论】:

      【解决方案5】:
      you can try the following:
      
      echo "The associative array<br>";
      $computer=array("brand"=>"hp", "price"=>"800", "cpu"=>"core i7");
      $keys=array_keys($computer);
      echo "<table><tr>";
      foreach($keys as $row){
          echo "<th style=\"border: solid 2px green\">".$row."</th>";
      }echo "</tr><tr>";
      foreach($computer as $col){
          echo "<td style=\"border: solid 2px blue\">".$col."</td>";
      }echo "</tr></table>";
      

      【讨论】:

        猜你喜欢
        • 2021-01-12
        • 1970-01-01
        • 2021-01-10
        • 2016-12-11
        • 2019-08-13
        • 2016-11-12
        • 2014-05-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多