亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何正確回顯這些 foreach 數組?

如何正確回顯這些 foreach 數組?

PHP
尚方寶劍之說 2022-07-29 10:26:14
在一個項目上工作,我對 PHP 很陌生,不知道如何以更好的方式做到這一點。    $item_thumbnails = array(        "https://brkcdn.com/v2/images/shop/thumbnails/f7dd8e95-551f-5e1f-92f1-2ea6c29e61ca.png",        "https://brkcdn.com/v2/images/shop/thumbnails/9fd1ea9d-cfd0-525a-963f-0c3438c5dc34.png"    );    $item_names = array(        "brick-luke's Fedora",        "Golden Bok"    );    $item_urls = array(        "https://www.brick-hill.com/shop/20/",        "https://www.brick-hill.com/shop/10267/"    );    $item_values = array(        "30.000",        "100.000"    );    $item_demands = array(        "High",        "Low"    );    foreach ($item_urls as $key => $item_url) {    foreach ($item_thumbnails as $key => $item_thumbnail) {}    foreach ($item_names as $key => $item_name) {}    foreach ($item_values as $key => $item_value) {}    foreach ($item_demands as $key => $item_demand) {}        echo ('        <div class="card">            <a class="image" href="'.$item_url.'">                <img src="'.$item_thumbnail.'" onerror="this.onerror=null;this.src="'.$storageUrl.'/error.png"">            </a>            <div class="content">            <a class="header">'.$item_name.'</a>                <div class="ui divider"></div>                Value: <div class="ui right floated red horizontal label">'.$item_value.'</div>                <div class="ui divider"></div>                Demand: <div class="ui right floated green horizontal label">'.$item_demand.'</div>            </div>        </div>        ');    }它僅在所有選項卡上顯示數組中的最新項目(如果有更多)。如何解決這個問題?幫助表示贊賞。
查看完整描述

2 回答

?
慕后森

TA貢獻1802條經驗 獲得超5個贊

像這樣的東西應該工作:


$item_thumbnails = array(

    "https://brkcdn.com/v2/images/shop/thumbnails/f7dd8e95-551f-5e1f-92f1-2ea6c29e61ca.png",

    "https://brkcdn.com/v2/images/shop/thumbnails/9fd1ea9d-cfd0-525a-963f-0c3438c5dc34.png"

);


$item_names = array(

    "brick-luke's Fedora",

    "Golden Bok"

);


$item_urls = array(

    "https://www.brick-hill.com/shop/20/",

    "https://www.brick-hill.com/shop/10267/"

);


$item_values = array(

    "30.000",

    "100.000"

);


$item_demands = array(

    "High",

    "Low"

);


// iterate over one array and get values from 

// other arrays under the same key `$key`

foreach ($item_urls as $key => $item_url) {

    echo ('

    <div class="card">

        <a class="image" href="'.$item_url.'">

            <img src="' . $item_thumbnails[$key] . '" onerror="this.onerror=null;this.src="'.$storageUrl.'/error.png"">

        </a>

        <div class="content">

        <a class="header">' . $item_names[$key] . '</a>

            <div class="ui divider"></div>

            Value: <div class="ui right floated red horizontal label">' . $item_values[$key] . '</div>

            <div class="ui divider"></div>

            Demand: <div class="ui right floated green horizontal label">' . $item_demands[$key] . '</div>

        </div>

    </div>

    ');

}


查看完整回答
反對 回復 2022-07-29
?
喵喵時光機

TA貢獻1846條經驗 獲得超7個贊

首先你應該改進你的數組結構。


$items = array(

  [

    "thumbnail" => "https://brkcdn.com/v2/images/shop/thumbnails/f7dd8e95-551f-5e1f-92f1-2ea6c29e61ca.png"

    "name" => "brick-luke's Fedora",

    "url" => "https://www.brick-hill.com/shop/20/",

    "value" => "30.000",

    "demand" => "High"

  ],

  [

    "thumbnail" => "https://brkcdn.com/v2/images/shop/thumbnails/9fd1ea9d-cfd0-525a-963f-0c3438c5dc34.png",

    "name" => "Golden Bok",

    "url" => "https://www.brick-hill.com/shop/10267/",

    "value" => "100.000",

    "demand" => "Low"

  ]

);

之后,您可以 foreach $items變量并從中獲取數據。見下文:


<?php

foreach ($items as $item) {

?>


<div class="card">

  <a class="image" href="<?php echo $item['url']; ?>">

    <img src="<?php echo $item['thumbnail']; ?>" onerror="this.onerror=null;this.src=<?php echo $storageUrl; ?>/error.png">

  </a>

  <div class="content">

    <a class="header"><?php echo $item['name']; ?></a>

    <div class="ui divider"></div>

    Value: <div class="ui right floated red horizontal label"><?php echo $item['value']; ?></div>

    <div class="ui divider"></div>

    Demand: <div class="ui right floated green horizontal label"><?php echo $item['demand']; ?></div>

  </div>

</div>


<?php

}

?>


查看完整回答
反對 回復 2022-07-29
  • 2 回答
  • 0 關注
  • 134 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號