如何使用 foreach 循环以不同于所有其他元素的方式呈现数组的第一个元素?

问题描述 投票:0回答:1

我有一个数组数据,例如这样的数组数据:

[0, 1, 2, 3, 4];

我想foreach数组数据来查看。我的第一个数组数据将放置标题,第二个数据将放置列表数据。这是我的看法:

<div class="editor-choice">
<div class="headsection">EDITOR’S CHOICE</div>
<!-- Headline Editor Choice -->
<article class="headline-editor-choice">
    <a href="https://www.sportstars.id/read/ole-gunnar-solskjaer-pelatih-man-united-terburuk-setelah-era-sir-alex-ferguson-48Gz3V">
        <div class="img-headline-editor-choice">
            <img src="https://img.sportstars.id/mpi/800//2021/11/48Gz3V/master_94xHM5T8r5_1941_ole_gunnar_solskjaer.jpg"
                alt="img-news">
        </div>
    </a>
    <div class="detail-headline-editor-choice">
        <a href="https://www.sportstars.id/ligainggris" class="category pb-0">Liga Inggris</a>
        <span class="date">22 November 2021 13:21 WIB</span>
        <div class="title-article title-editor-choice-headline">
            <a href="https://www.sportstars.id/read/ole-gunnar-solskjaer-pelatih-man-united-terburuk-setelah-era-sir-alex-ferguson-48Gz3V"
                class="title" style="-webkit-box-orient: vertical;">Ole Gunnar Solskjaer Pelatih Man United Terburuk
                Setelah
                Era Sir Alex Ferguson</a>
        </div>
    </div>
</article>
<!-- List Editor Choice -->
<?php foreach ($editorchoice as $kuy => $val) { ?>
<article class="list-editor-choice">
    <div class="img-editor-choice">
        <a
            href="<?php echo $val['link']; ?>">
            <img src="<?php echo image_uri().'/okz/900/'.str_replace("thumb","master",@$val['image_content']); ?>"
                alt="img-news">
        </a>
    </div>
    <div class="detail-editor-choice">
        <div class="title-article title-editor-choice">
            <a href="<?php echo $val['link']; ?>"
                class="title" style="-webkit-box-orient: vertical;"><?php echo $val['title']; ?></a>
        </div>
        <div class="warp-date-article">
            <div class="ico-calendar"><img src="<?php echo base_url(); ?>assets/mobile/images/icon/calender.svg">
            </div>
            <div class="date-article">19 November 2021</div>
        </div>
    </div>
</article>
<?php } ?>

这是编辑器选择数据的示例(来自数组的数据):

array data example

如何将$editorchoice中的第一个数组数据制作为Headline Editor Choice,将第二个及其他数据制作为List Editor Choice?

我使用Codeigniter 3

php loops codeigniter
1个回答
0
投票

我用这段代码解决了这个问题:

<?php if(!empty($editorchoice)):?>
    <!-- Editor Choice -->
    <div class="editor-choice">
        <div class="headsection">EDITOR’S CHOICE</div>
        <!-- Headline Editor Choice -->
        <article class="headline-editor-choice">
            <a href="<?php echo $editorchoice[0]['link']; ?>">
                <div class="img-headline-editor-choice">
                    <img src="<?php echo image_uri().'mpi/800/'.str_replace("thumb","master",$editorchoice[0]['image_content']); ?>" alt="img-news">
                </div>
            </a>
            <div class="detail-headline-editor-choice">
                <a href="<?php echo site_url(channel_link(@$editorchoice[0]['channel_name'])); ?>" class="category pb-0"><?php echo @$editorchoice[0]['channel_name']; ?></a>
                <span class="date"><?php echo $editorchoice[0]['timeago']; ?></span>
                <div class="title-article title-editor-choice-headline">
                    <a href="<?php echo $editorchoice[0]['link']; ?>" class="title"><?php echo $editorchoice[0]['title']; ?></a>
                </div>
            </div>

        </article>
        <!-- List Editor Choice -->
        <?php $i = 0; foreach ($editorchoice as $key => $value) { ?>
        <?php if(!empty($i)):?>
        <article class="list-editor-choice">
            <div class="img-editor-choice">
                <a href="<?php echo $value['link']; ?>">
                    <img src="<?php echo image_uri().'mpi/800/'.str_replace("thumb","master",$value['image_content']); ?>" alt="img-news">
                </a>
            </div>
            <div class="detail-editor-choice">
                <div class="title-article title-editor-choice">
                    <a href="<?php echo $value['link']; ?>" class="title"><?php echo $value['title']; ?></a>
                </div>
                <div class="warp-date-article">
                    <div class="ico-calendar"><img src="<?php echo base_url()?>assets/mobile/images/icon/calender.svg"></div>
                    <div class="date-article"><?php echo @$value['timeago']; ?></div>
                </div>
            </div>
        </article>
        <?php endif;?>
        <?php $i++; } ?>
    </div>
    <!-- End Editor Choice -->

谢谢你..

© www.soinside.com 2019 - 2024. All rights reserved.