doumiebiao6827 2018-03-06 02:49
浏览 18
已采纳

如何使用php排除数组中的最后一行

I've been trying to read up on this in the manual, but basically I've got an array where I'm trying to reverse it and exclude the last item. So I got 14 items currently in my array and I'm getting it to reverse and it shows 14-2. My code got it to exclude the last item. So I guess it technically works, but I actually want it to output as 13-1. I tried using array_pop and array_shift but I didn't know how to integrate it with array_reverse.

function smile_gallery( $atts ) {
    if( have_rows('smile_gallery', 3045) ):

    $i = 1;

    $html_out = '';

    $html_out .= '<div class="smile-container">';
        $html_out .= '<div class="fg-row row">';

            // vars
            $rows = get_field('smile_gallery', 3045);
            $count = count( get_field('smile_gallery', 3045) );

            $html_out .= '<div class="col-md-8">'; // col-md-offset-1
                $html_out .= '<div class="smile-thumbs">';

                foreach( array_reverse($rows) as $row) :

                // vars
                $week = "smile_week";
                $img = "smile_img";
                $caption = "smile_caption";

                // Do stuff with each post here
                if( $i < $count) :

                    $html_out .= '<div class="smile-thumb-container">';
                        $html_out .= '><h6>Week ' . $row["smile_week"] . '</h6></a>'; // smile thumb week  
                    $html_out .= '</div>'; // smile thumb container
                endif;

                $i++;

                endforeach;

                $html_out .= '</div>';
            $html_out .= '</div>';
        $html_out .= '</div>';
    $html_out .= '</div>'; // smile container

    endif;

    return $html_out;
}
add_shortcode( 'show_smiles', 'smile_gallery' );
  • 写回答

1条回答 默认 最新

  • drwf69817 2018-03-06 03:35
    关注

    I'm reading your question as the following, correct me if I'm wrong.

    I've got an array where I'm trying to reverse it and exclude the first and last items.

    To do that as you know you're want to use array_pop() and array_shift().

    <?php
    //
    $rows = get_field('smile_gallery', 3045);
    $count = count($rows);
    
    array_pop($rows);
    array_shift($rows);
    
    foreach (array_reverse($rows) as $row):
    ...
    

    If you want to reverse first and then do your operations, which is not required if your removing items from both ends. Take out array_reverse from the foreach and then do your manipulations.

    <?php
    // vars
    $rows = get_field('smile_gallery', 3045);
    $count = count($rows);
    
    $rows = array_reverse($rows);
    
    array_pop($rows);
    array_shift($rows);
    
    foreach ($rows as $row):
    ...
    

    Let me know if that helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?