如何输出列表的倒数

时间:2018-08-22 07:00:20

标签: php

我有以下代码,我想以相反的顺序显示它:

                <div id="gallery-1" class="royalSlider rsDefault visibleNearby">


            <?php
            $current_item = 1;
            $imgID = 1;

            foreach( $cats as $key => $item ){

            if( !empty($cat_id) && $cat_id == $item->id ){
                $current_item = $key+1;
            }?>

              <img class="rsImg" src="<?php echo cmMedia::getOriginalUrl($item->media_id);?>" id="imgID<?php echo $imgID; ?>" />
              <a class="rsImg" href="<?php echo cmMedia::getOriginalUrl($item->media_id);?>" id="imgID<?php echo $imgID; ?>" /></a>

            <?php $imgID++; }?>



            </div>

输出是带有26个幻灯片的滑块。

我尝试用-翻转+,然后将变量从26开始设置为1,但没有成功:/

提前谢谢!

2 个答案:

答案 0 :(得分:5)

我认为您正在寻找array_reversehttp://php.net/manual/en/function.array-reverse.php

<?php
  $current_item = 1;
  $imgID = 1;
  $cats = array_reverse($cats);
  //... the rest should be the same as you have

答案 1 :(得分:1)

array_reverse很好,另一种方法是将foreach换成for倒转的循环:

for ($key = count($cats) - 1; $key >= 0; $key--) {
    $item = $cats[$key];

    // Proceed with the original code
}