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

how to use php to exclude the last row in an array

我一直在尝试阅读手册中的相关内容,但基本上我有一个 array 试图反转它并排除最后一项。所以我的数组中目前有 14 个项目,我正在将其反转并显示 14-2。我的代码让它排除了最后一项。所以我猜它在技术上是可行的,但我实际上希望它输出为 13-1。我尝试使用 array_poparray_shift 但我不知道如何将它与 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' );

我正在阅读你的问题如下,如果我错了请纠正我。

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

如您所知,要做到这一点,您需要使用 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):
...

如果你想先反转再操作,如果你从两端删除项目,则不需要。从 foreach 中取出 array_reverse 然后进行操作。

<?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):
...

如果有帮助请告诉我。