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.

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

报告相同问题?

悬赏问题

  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面