如何将PHP对象定义为数组中的最后一项?

时间:2022-10-24 17:23:32

In the code below I want to apply the object "$activeclass" as a DIV class. I thought the end pointer I included would apply this only to the last iteration of the array, but instead it is applying the class to all iterations.

在下面的代码中,我想将对象“$activeclass”应用于DIV类。我认为我所包含的结束指针只适用于数组的最后一次迭代,而是将该类应用于所有迭代。

<div id="right_bottom">
                <?
                $content = is_array($pagedata->content) ? $pagedata->content : array($pagedata->content);
                foreach($content as $item){
                $activeclass = end($content) ? 'active' : ' ';
                    ?>
                    <div id="right_side">
                           <div id="<?=$item->id?>" class="side_items <?=$activeclass?>">
                             <a class="content" href="<?=$item->id?>"><img src="<?=PROTOCOL?>//<?=DOMAIN?>/img/content/<?=$item->image?>"><br />
                             <strong><?=$item->title?></strong></a><br />
                             <h2><?=date('F j, Y',  strtotime($item->published))?></h2><br />
                         </div>
                    </div>
                    <?
                }
                ?>
</div>

Any ideas where I am making a mistake? How can I apply the class $activeclass only to the last iteration of my "foreach" statement?

我有什么想法吗?我如何在“foreach”语句的最后一个迭代中应用类$activeclass ?

3 个解决方案

#1


3  

The simplest way is to keep a count:

最简单的方法是保持计数:

$i = 0; $size = count( $content);
foreach( $content as $item) {
    $i++;
    $activeclass = ( $i < $size) ? '' : 'active';
}

Alternatively, you can compare the last element with the current element (if your array is consecutively numerically indexed starting with 0 [Thanks to webbiedave for pointing out the assumptions made by this method]):

或者,您可以将最后一个元素与当前元素进行比较(如果您的数组从0开始连续地从数字索引开始[感谢webbiedave指出这个方法的假设]):

$last = count( $content) - 1;
foreach( $content as $item) {
    $activeclass = ( $content[$last] === $item) ? 'active' : '';
}

Note that this approach will not work if your array has duplicate items.

请注意,如果数组中有重复项,此方法将不起作用。

Finally, you can compare indexes in the following way:

最后,您可以通过以下方式比较索引:

// Numerical or associative
$keys = array_keys($content); 
$key = array_pop($keys); // Assigned to variables thanks to webbiedave

// Consecutive numerically indexed
$key = count( $content) - 1; 

foreach( $content as $current_key => $item) {
    $activeclass = ( $current_key === $key) ? 'active' : '';
}

#2


1  

$activeclass = end($content) ? 'active' : ' ';

The end() function returns the last element in the array, so you're basically checking to see if the array has a last element (which it always will, unless it's empty).

函数的作用是:返回数组中的最后一个元素,所以你基本上是在检查数组是否有最后一个元素(除非它是空的)。

This is an explanation of what you're doing wrong - nick has the answer for how to fix it by using a counter.

这是对你所做的错误的解释——尼克用一个计数器来解决这个问题。

#3


0  

Try this approach from the following link http://blog.actsmedia.com/2009/09/php-foreach-last-item-last-loop/

试试下面的链接:http://blog.actsmedia.com/2009/09/php-foreach-last- loop/。

$last_item = end($array);
$last_item = each($array);
reset($array);
foreach($array as $key => $value)
{
    // code executed during standard iteration
    if($value == $last_item['value'] && $key == $last_item['key'])
    {
    // code executed on the 
            // last iteration of the foreach loop
    }
 }

#1


3  

The simplest way is to keep a count:

最简单的方法是保持计数:

$i = 0; $size = count( $content);
foreach( $content as $item) {
    $i++;
    $activeclass = ( $i < $size) ? '' : 'active';
}

Alternatively, you can compare the last element with the current element (if your array is consecutively numerically indexed starting with 0 [Thanks to webbiedave for pointing out the assumptions made by this method]):

或者,您可以将最后一个元素与当前元素进行比较(如果您的数组从0开始连续地从数字索引开始[感谢webbiedave指出这个方法的假设]):

$last = count( $content) - 1;
foreach( $content as $item) {
    $activeclass = ( $content[$last] === $item) ? 'active' : '';
}

Note that this approach will not work if your array has duplicate items.

请注意,如果数组中有重复项,此方法将不起作用。

Finally, you can compare indexes in the following way:

最后,您可以通过以下方式比较索引:

// Numerical or associative
$keys = array_keys($content); 
$key = array_pop($keys); // Assigned to variables thanks to webbiedave

// Consecutive numerically indexed
$key = count( $content) - 1; 

foreach( $content as $current_key => $item) {
    $activeclass = ( $current_key === $key) ? 'active' : '';
}

#2


1  

$activeclass = end($content) ? 'active' : ' ';

The end() function returns the last element in the array, so you're basically checking to see if the array has a last element (which it always will, unless it's empty).

函数的作用是:返回数组中的最后一个元素,所以你基本上是在检查数组是否有最后一个元素(除非它是空的)。

This is an explanation of what you're doing wrong - nick has the answer for how to fix it by using a counter.

这是对你所做的错误的解释——尼克用一个计数器来解决这个问题。

#3


0  

Try this approach from the following link http://blog.actsmedia.com/2009/09/php-foreach-last-item-last-loop/

试试下面的链接:http://blog.actsmedia.com/2009/09/php-foreach-last- loop/。

$last_item = end($array);
$last_item = each($array);
reset($array);
foreach($array as $key => $value)
{
    // code executed during standard iteration
    if($value == $last_item['value'] && $key == $last_item['key'])
    {
    // code executed on the 
            // last iteration of the foreach loop
    }
 }