猿问

如何正确回显这些 foreach 数组?

在一个项目上工作,我对 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>

        ');

    }

它仅在所有选项卡上显示数组中的最新项目(如果有更多)。


如何解决这个问题?帮助表示赞赏。


尚方宝剑之说
浏览 112回答 2
2回答

慕后森

像这样的东西应该工作:$item_thumbnails = array(&nbsp; &nbsp; "https://brkcdn.com/v2/images/shop/thumbnails/f7dd8e95-551f-5e1f-92f1-2ea6c29e61ca.png",&nbsp; &nbsp; "https://brkcdn.com/v2/images/shop/thumbnails/9fd1ea9d-cfd0-525a-963f-0c3438c5dc34.png");$item_names = array(&nbsp; &nbsp; "brick-luke's Fedora",&nbsp; &nbsp; "Golden Bok");$item_urls = array(&nbsp; &nbsp; "https://www.brick-hill.com/shop/20/",&nbsp; &nbsp; "https://www.brick-hill.com/shop/10267/");$item_values = array(&nbsp; &nbsp; "30.000",&nbsp; &nbsp; "100.000");$item_demands = array(&nbsp; &nbsp; "High",&nbsp; &nbsp; "Low");// iterate over one array and get values from&nbsp;// other arrays under the same key `$key`foreach ($item_urls as $key => $item_url) {&nbsp; &nbsp; echo ('&nbsp; &nbsp; <div class="card">&nbsp; &nbsp; &nbsp; &nbsp; <a class="image" href="'.$item_url.'">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <img src="' . $item_thumbnails[$key] . '" onerror="this.onerror=null;this.src="'.$storageUrl.'/error.png"">&nbsp; &nbsp; &nbsp; &nbsp; </a>&nbsp; &nbsp; &nbsp; &nbsp; <div class="content">&nbsp; &nbsp; &nbsp; &nbsp; <a class="header">' . $item_names[$key] . '</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="ui divider"></div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Value: <div class="ui right floated red horizontal label">' . $item_values[$key] . '</div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="ui divider"></div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Demand: <div class="ui right floated green horizontal label">' . $item_demands[$key] . '</div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; &nbsp; ');}

喵喵时光机

首先你应该改进你的数组结构。$items = array(&nbsp; [&nbsp; &nbsp; "thumbnail" => "https://brkcdn.com/v2/images/shop/thumbnails/f7dd8e95-551f-5e1f-92f1-2ea6c29e61ca.png"&nbsp; &nbsp; "name" => "brick-luke's Fedora",&nbsp; &nbsp; "url" => "https://www.brick-hill.com/shop/20/",&nbsp; &nbsp; "value" => "30.000",&nbsp; &nbsp; "demand" => "High"&nbsp; ],&nbsp; [&nbsp; &nbsp; "thumbnail" => "https://brkcdn.com/v2/images/shop/thumbnails/9fd1ea9d-cfd0-525a-963f-0c3438c5dc34.png",&nbsp; &nbsp; "name" => "Golden Bok",&nbsp; &nbsp; "url" => "https://www.brick-hill.com/shop/10267/",&nbsp; &nbsp; "value" => "100.000",&nbsp; &nbsp; "demand" => "Low"&nbsp; ]);之后,您可以 foreach $items变量并从中获取数据。见下文:<?phpforeach ($items as $item) {?><div class="card">&nbsp; <a class="image" href="<?php echo $item['url']; ?>">&nbsp; &nbsp; <img src="<?php echo $item['thumbnail']; ?>" onerror="this.onerror=null;this.src=<?php echo $storageUrl; ?>/error.png">&nbsp; </a>&nbsp; <div class="content">&nbsp; &nbsp; <a class="header"><?php echo $item['name']; ?></a>&nbsp; &nbsp; <div class="ui divider"></div>&nbsp; &nbsp; Value: <div class="ui right floated red horizontal label"><?php echo $item['value']; ?></div>&nbsp; &nbsp; <div class="ui divider"></div>&nbsp; &nbsp; Demand: <div class="ui right floated green horizontal label"><?php echo $item['demand']; ?></div>&nbsp; </div></div><?php}?>
随时随地看视频慕课网APP
我要回答