简体   繁体   English

PHP在HTML表中显示关联数组

[英]PHP display associative array in HTML table

Here is my associative array: 这是我的关联数组:

$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");

Here is my sorting algorithm: 这是我的排序算法:

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);

I am displaying the data after sorting: 我在排序后显示数据:

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

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

My problem now is that I want to put that array into an HTML table but dont know how. 我现在的问题是我想将该数组放入HTML表中,但不知道如何做。 This is my code which I tried so far but its not working: 这是我到目前为止尝试过的代码,但是没有用:

$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>";
?>

Could any one tell me what is wrong with my code, or how I can display this sorted data into a table? 谁能告诉我我的代码有什么问题,或者如何将排序后的数据显示在表中? Thanks. 谢谢。

Why not just modify the loop you already use to display the data? 为什么不只修改您已经用于显示数据的循环?

echo "<table>";
foreach($req_data as $key=>$row) {
    echo "<tr>";
    foreach($row as $key2=>$row2){
        echo "<td>" . $row2 . "</td>";
    }
    echo "</tr>";
}
echo "</table>";
$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;

Sorry for the necro, but I was actually looking to see if there was a build-in function for this as I was writing this. 对不起,坏死了,但是我实际上在写这篇文章的时候想看看是否有内置函数。

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; } }
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>";
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>";?>

Try something like this 试试这个

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>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM