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

循环遍历 PHP 数组而不是手动

循环遍历 PHP 数组而不是手动

PHP
撒科打诨 2023-07-30 13:44:12
所以我了解了 for 循环、do while、foreach,但我只是不知道如何编写代码,因此循环会自动创建我现在手动执行的信息(只需复制和意大利面)请参阅下面的代码我的问题:-)如何循环?我可能必须创建一个 Matrix 数组或如何调用 -) 如何将 img 链接包含在数组中并将其插入带有循环的 html 代码中<?php// Available, Name , Model,  Price, Rent Location, $car1 = array(    "available" => "Available",    "name" => "Duster",     "model" =>"Nissan",      "price" => 333,     "location" => "Hollywood");     $car2 = array(        "available" => "Available",        "name" => "Panzer",         "model" =>"Bundesheer",          "price" => 98753,         "location" => "Kaserne");         $car3 = array(            "available" => "NOT Available",            "name" => "Hubschrauber",             "model" =>"Pew Pew",              "price" => 98756,             "location" => "Dschungel");             $car4 = array(                "available" => "Available",                "name" => "Schiff",                 "model" =>"Öltanker",                  "price" => 223123213132,                 "location" => "Baltisches Meer");     echo '          <div class="card" style="width: 18rem;">  <img class="card-img-top" src="https://cdn.pixabay.com/photo/2018/09/15/12/33/nissan-3679299_1280.jpg" alt="Card image cap">  <div class="card-body">    <h5 class="card-title">'.$car1["name"]. ' </h5>    <p class="card-text">Model: '.$car1["model"]. '<br> Preis: '.$car1["price"]. '<br>Location: '.$car1["location"]. '</p>    <p class="card-text"><b>'.$car1["available"]. '</b></p>    <a href="#" class="btn btn-primary">Order NOW</a>  </div></div>     
查看完整描述

1 回答

?
犯罪嫌疑人X

TA贡献2080条经验 获得超4个赞

您需要创建一个多维数组(数组的数组),

一旦你有了数组,你就可以迭代它。

$cars = [

    [

        "available" => "Available",

        "name" => "Duster",

        "model" =>"Nissan",

        "price" => 333,

        "location" => "Hollywood"

    ],

    [

        "available" => "Available",

        "name" => "Schiff",

        "model" =>"Öltanker",

        "price" => 223123213132,

        "location" => "Baltisches Meer"

    ],

    [

        "available" => "NOT Available",

        "name" => "Hubschrauber",

        "model" =>"Pew Pew",

        "price" => 98756,

        "location" => "Dschungel"

    ],

    [

        "available" => "Available",

        "name" => "Panzer",

        "model" =>"Bundesheer",

        "price" => 98753,

        "location" => "Kaserne"

    ],

];


foreach ($cars as $car) {

    echo $car['available'];

}


查看完整回答
反对 回复 2023-07-30
  • 1 回答
  • 0 关注
  • 121 浏览

添加回答

举报

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