【发布时间】:2022-02-15 17:24:58
【问题描述】:
我需要从 div 中的数组中显示 3 个不同的标题。我该怎么做?我有变量 $str - 我试图从 $articles 显示 $title 但实际上它只是显示 $str 3 次。变量 $title 在
<?php
$articles = array(
array(
"title" => "Featured title 1",
"description" => "Paragraph of text beneath the heading to explain the heading. We'll add onto it with another sentence and probably just keep going until we run out of words.",
"link" => "page1.php",
),
array(
"title" => "Featured title 2",
"description" => "Paragraph of text beneath the heading to explain the heading. We'll add onto it with another sentence and probably just keep going until we run out of words.",
"link" => "page2.php",
),
array(
"title" => "Featured title 3",
"description" => "Paragraph of text beneath the heading to explain the heading. We'll add onto it with another sentence and probably just keep going until we run out of words.",
"link" => "page2.php",
),
);
$title = 'Paragraph of text';
$str = '
<div class="feature col">
<div class="feature-icon bg-primary bg-gradient">
<svg class="bi" width="1em" height="1em"><use xlink:href="#collection"></use></svg>
</div>
<h2>'.$title.'</h2>
<p>Paragraph of text beneath the heading to explain the heading. We\'ll add onto it with another sentence and probably just keep going until we run out of words.</p>
<a href="#" class="icon-link">
Call to action
<svg class="bi" width="1em" height="1em"><use xlink:href="#chevron-right"></use></svg>
</a>
</div>
';
foreach ($articles as $v){
echo $str;
}
【问题讨论】:
-
你想在你的循环中移动你的“$str = ....”并将
$title = $v['title'];放在上面。