PHP在HTML表格中显示关联数组

时间:2011-07-27 04:47:42

标签: php html 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");

这是我的排序算法:

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

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

5 个答案:

答案 0 :(得分:4)

为什么不修改已用于显示数据的循环?

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

答案 1 :(得分:1)

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

很抱歉这个死神,但实际上我正在寻找是否有这样的内置功能,因为我写这篇文章。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

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