【问题标题】:foreach title in array php数组php中的foreach标题
【发布时间】: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'];放在上面。

标签: php html css


【解决方案1】:

您需要从循环数组中加载$title 变量

$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){
    $title = $var['title'];
    echo $str;
}

【讨论】:

  • 它有效!谢谢!!!!!!
猜你喜欢
  • 1970-01-01
  • 2015-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-01
  • 2018-09-11
  • 2012-05-01
  • 2021-04-14
相关资源
最近更新 更多