表格格式的数组视图

时间:2017-09-29 09:08:52

标签: php html

如何以表格格式显示数组

数组是

        Array
    (
        [0] => Array
            (
                [MarketName] => BTC-1ST
                [High] => 9.55E-5
                [Low] => 8.5E-5
                [Volume] => 1557119.52168
                [Last] => 8.75E-5
                [BaseVolume] => 138.34919019
                [TimeStamp] => 2017-09-29T09:03:03.743
                [Bid] => 8.735E-5
                [Ask] => 8.765E-5
                [OpenBuyOrders] => 409
                [OpenSellOrders] => 5994
                [PrevDay] => 8.56E-5
                [Created] => 2017-06-06T01:22:35.727
            )

        [1] => Array
            (
                [MarketName] => BTC-2GIVE
                [High] => 1.56E-6
                [Low] => 1.45E-6
                [Volume] => 3448220.38871
                [Last] => 1.5E-6
                [BaseVolume] => 5.1828602
                [TimeStamp] => 2017-09-29T08:55:45.413
                [Bid] => 1.49E-6
                [Ask] => 1.5E-6
                [OpenBuyOrders] => 363
                [OpenSellOrders] => 1976
                [PrevDay] => 1.48E-6
                [Created] => 2016-05-16T06:44:15.287
            )
)

不仅有两个数组....我有很多数组....但是锄头可以在表格中显示...... ??

我希望像这样显示 enter image description here

我试过这段代码

<table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th colspan="2" class="text-center">Buy Orders</th>
                                    </tr>
                                </thead>
                                <tbody>                                     
                                    <tr>
                                        <td align="center"><b>Market</b></td>
                                        <td align="center"><b>High</b></td>
                                    </tr>

                                    <tr>
                                        <td align="center"><?php echo $b[0]['MarketName'];?></td>
                                        <td align="center"><?php echo $b[0]['High'];?></td>
                                    </tr>

                                </tbody>
                            </table>

我希望以这种格式显示所有数组视图,例如$ result [0],$ result 1,...

2 个答案:

答案 0 :(得分:1)

详细了解:

每个https://www.w3schools.com/php/php_looping.asp

数组https://www.w3schools.com/php/php_arrays.asp

多维数组https://www.w3schools.com/php/php_arrays_multi.asp

<table class="table table-hover">
<thead>
    <tr>
        <th colspan="2" class="text-center">Buy Orders</th>
    </tr>
</thead>
<tbody>                                     
    <tr>
        <td align="center"><b>Market</b></td>
        <td align="center"><b>High</b></td>
    </tr>

    <tr>
        <?php
        foreach ($result as $k=>$v)
        {
        ?>
        <td align="center"><?php echo $result[$k]['MarketName'];?></td>
        <td align="center"><?php echo $result[$k]['High'];?></td>
        <?php 
        }
        ?>
    </tr>

</tbody>
</table>

答案 1 :(得分:0)

您必须使用foreach循环来遍历记录并在表格行中显示每个记录。

从您的代码中为您创建此示例

$records = [
    [
    "MarketName"     => "BTC-1ST",
    "High"           => "9.55E-5",
    "Low"            => "8.5E-5",
    "Volume"         => "1557119.52168",
    "Last"           => "8.75E-5",
    "BaseVolume"     => "138.34919019",
    "TimeStamp"      => "2017-09-29T09:03:03.743",
    "Bid"            => "8.735E-5",
    "Ask"            => "8.765E-5",
    "OpenBuyOrders"  => "409",
    "OpenSellOrders" => "5994",
    "PrevDay"        => "8.56E-5",
    "Created"        => "2017-06-06T01:22:35.727",
    ],[
    "MarketName"     => "BTC-2GIVE",
    "High"           => "1.56E-6",
    "Low"            => "1.45E-6",
    "Volume"         => "3448220.38871",
    "Last"           => "1.5E-6",
    "BaseVolume"     => "5.1828602",
    "TimeStamp"      => "2017-09-29T08:55:45.413",
    "Bid"            => "1.49E-6",
    "Ask"            => "1.5E-6",
    "OpenBuyOrders"  => "363",
    "OpenSellOrders" => "1976",
    "PrevDay"        => "1.48E-6",
    "Created"        => "2016-05-16T06:44:15.287",
    ],
];

echo "<table border='1'>";
echo "
<tr>
    <th>heading</th>
    <th>heading</th>
    <th>heading</th>
    <th>heading</th>
    <th>heading</th>
    <th>heading</th>
    <th>heading</th>
    <th>heading</th>
    <th>heading</th>
    <th>heading</th>
    <th>heading</th>
    <th>heading</th>
    <th>heading</th>
</tr>
";
foreach($records as $value){
echo "
<tr>
    <td>{$value["MarketName"]}</td>
    <td>{$value["High"]}</td>
    <td>{$value["Low"]}</td>
    <td>{$value["Volume"]}</td>
    <td>{$value["Last"]}</td>
    <td>{$value["BaseVolume"]}</td>
    <td>{$value["TimeStamp"]}</td>
    <td>{$value["Bid"]}</td>
    <td>{$value["Ask"]}</td>
    <td>{$value["OpenBuyOrders"]}</td>
    <td>{$value["OpenSellOrders"]}</td>
    <td>{$value["PrevDay"]}</td>
    <td>{$value["Created"]}</td>
</tr>
";
}
echo "</table>";

示例: https://3v4l.org/Wpqnb