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

所以我了解了 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>

     

撒科打诨
浏览 122回答 1
1回答

犯罪嫌疑人X

您需要创建一个多维数组(数组的数组),一旦你有了数组,你就可以迭代它。$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'];}
打开App,查看更多内容
随时随地看视频慕课网APP