将PHP阵列导入引导卡

时间:2018-08-09 20:03:03

标签: php

我有一些JSON,$ json。 这是用json_decode解析的,给了我一个可以提取值的数组。对于此数组中的每个项目,如何将其值移入引导卡?

<?php
    $json = json_decode($content, true);
    foreach($json['response']['results'] as $response){
        echo $response['fields']['headline'] . "<br/>";
        echo $response['fields']['trailText'] . "<br/>";
        echo $response['fields']['thumbnail'] . "<br/>";
        echo $response['fields']['shortUrl'] . "<br/></br>";
    }
?>

引导卡示例:

<div class="card" style="max-width:319px;">
    <img src="myImage.png" class="card-img-top" alt="Card image" />
    <div class="card-block">
        <h4 class="card-title">This is a title</h4>
        <p class="card-text">Card text</p>
        <a href="http://website.co.uk" class="btn btn-success">Button</a>
    </div>
</div> 

2 个答案:

答案 0 :(得分:1)

只需在foreach循环中回显html并添加字段值

<?php
    $json = json_decode($content, true);
    foreach($json['response']['results'] as $response){
        echo '<div class="card" style="max-width:319px;">
            <img src="' . $response['fields']['thumbnail'] . '" class="card-img-top" alt="Card image" />
            <div class="card-block">
                <h4 class="card-title">' . $response['fields']['headline'] . '</h4>
                <p class="card-text">' . $response['fields']['trailText'] . '</p>
                <a href="' . $response['fields']['shortUrl'] . '" class="btn btn-success">Button</a>
            </div>
        </div>; 
    }
?>

答案 1 :(得分:1)

我会做这样的事情:

<?php
$json = json_decode($content, true);
foreach($json['response']['results'] as $response){
    ?>
    <div class="card" style="max-width:319px;">
        <img src="<?=$response['fields']['thumbnail']?>" class="card-img-top" alt="Card image" />
        <div class="card-block">
            <h4 class="card-title"><?=$response['fields']['headline']?></h4>
            <p class="card-text"><?=$response['fields']['trailText']?></p>
            <a href="<?=$response['fields']['shortUrl']?>" class="btn btn-success">Button</a>
        </div>
    </div> 
    <?php
}
?>

我认为更改更好,让我知道这是否对您有用!