为了账号安全,请及时绑定邮箱和手机立即绑定

如何将数组添加到我的 php 变量中,循环不适用于数组

如何将数组添加到我的 php 变量中,循环不适用于数组

PHP
慕村9548890 2022-01-24 10:44:12
我正在尝试将我的变量放入一个或多个数组中,然后循环它们<!DOCTYPE html><html><style>    html {        background-color: bisque;    }    h1 {        display: block;        text-align: center;    }    h2 {        display: block;        text-align: center;        color: saddlebrown;    }    img {        border-radius: 50%;        width: 800px;        text-align: center;    }    p {        text-align: center;        color: saddlebrown;    }</style><head>    <meta charset="UTF-8">    <title>Dynamic web pages with PHP</title></head><body>    <header>        <nav id="main-navigation">        </nav>    </header>    <?php      $Cartitle = "My favourite Cars";                    $Carname = "HRV";                    $Carprice = "CAD-$23300";                    $Carimg = "http://direct.automobiles.honda.com/images/2016/hr-v/exterior-gallery-new/2016-honda-hrv-front-view.jpg";                    $Cardescription = "HRV is a mini suv made by Honda";                   $Carname2 = "CHR";                   $Carprice2 = "CAD-$23675";                    $Carimg2 = "https://d1bxbrgbmu84na.cloudfront.net/wp-content/uploads/2019/08/16093812/CHR.jpg";                  $Cardescription2 = "HRV is a mini suv made by Toyota";                  $Carname3 = "RDX";                   $Carprice3 = "CAD-$43990";                    $Carimg3 = "https://www.acura.ca/Content/acura.ca/e270f141-7f67-4fe2-99bd-e808e3c3c2d7/MediumSizedFeature/03_rdx19_overview_MediumFeature_mobile.jpg";                  $Cardescription3 = "RDX is a large SUV made by Acura"; ?>    <?php    echo "<h1>$Cartitle<h2>$Carname <p>$Carprice <p> $Cardescription <br>  <img src=$Carimg></p></h2><h1><br>";     echo "<h2>$Carname2 <p>$Carprice2 <p> $Cardescription2 <br>  <img src=$Carimg2></p></h2><br>";    echo "<h2>$Carname3 <p>$Carprice3 <p> $Cardescription3 <br>  <img src=$Carimg3></p></h2>";        ?></body></html>
查看完整描述

1 回答

?
DIEA

TA贡献1820条经验 获得超2个赞

作为示例(样式并根据需要添加)


$cars[] = ['name'  => 'HRV',

           'price' => 'CAD-$23300',

           'img'   => 'http://direct.automobiles.honda.com/images/2016/hr-v/exterior-gallery-new/2016-honda-hrv-front-view.jpg',

           'desc'  => 'HRV is a mini suv made by Honda',

           ];

$cars[] = ['name'  => 'CHR' ,

           'price' => 'CAD-$23675' ,

           'img'   => 'https://d1bxbrgbmu84na.cloudfront.net/wp-content/uploads/2019/08/16093812/CHR.jpg' ,

           'desc'  => 'RDX is a large SUV made by Acura' ,

           ];


echo '<h1>My favourite Cars</h1>';


foreach ($cars as $detail) {


echo '<h2>' . $detail['name']  . '<p>' . 

              $detail['price'] . '<p>' . 

              $detail['desc']  . '<br>' . 

'<img src=' . $detail['img']   . '</p></h2>';


}


查看完整回答
反对 回复 2022-01-24
  • 1 回答
  • 0 关注
  • 149 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信