如何在php中打印数组?

时间:2019-05-31 12:33:53

标签: php html

我需要用html打印一些卡,但是我将收到一个php数组,其中包含必须打印的天数。 我已经尝试过了,但是没有用。

<div>
<div class="row" style="padding: 0px 30px">
    <?php foreach ($array as $num):?>
        <div class="col">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title"><?php $num ?></h3>
                </div>
                <div class="card-body">
                    <p class="card-text">With supporting text below as a natural lead-in to additional content.</p>
                </div>
            </div>
        </div>
    <?php endforeach ?>
</div>

2 个答案:

答案 0 :(得分:0)

我真的不明白你在问什么,但是 PHP中的echo数组,您必须提及要echo的索引
有两种方法可以回显所需的索引

注意:PHP中的索引从0开始

首先,如果索引没有名称:

$array=["one","two","three"];
    echo $array['0']; //the output will be "one"

第二种方法用于具有名称的索引:

$array=["first"=>"One","second"=>"two","third"=>"three"];
    echo $array['test'];

所以您的代码应该像这样

<div>
<div class="row" style="padding: 0px 30px">
    <?php foreach ($array as $num):?>
        <div class="col">
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title"><?php echo $num[/*the index you want */] ?></h3>
                </div>
                <div class="card-body">
                    <p class="card-text">With supporting text below as a natural lead-in to additional content.</p>
                </div>
            </div>
        </div>
    <?php endforeach; ?>
</div>

答案 1 :(得分:0)

也许这些循环之一会有所帮助:

foreach ($array as $mykey=>$myval) {
    echo $myval;
}

foreach ($array as $mykey=>$myval) {
    echo $array[$mykey];
}