PHP Foreach 如果数组最后

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9700012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:27:22  来源:igfitidea点击:

PHP Foreach If Array Last

phparraysif-statementforeach

提问by Kristian Matthews

foreach( $tabs2 as $tab2 => $name ){
    $class = ( $tab2 == $current ) ? ' current' : '';
    echo("<li class='posts'><a href='?page=pigg&tab=help&tab2=$tab2' class='$class'>$name");
    echo(' |'); // If array last then do not display
    echo('</a></li>');
}

I'm using a foreach loop to create a navigation for a WordPress plugin I'm working on, but I don't want the ' |' to be displayed for the last element, the code above is what I've got so far, I was thinking of using an if statement on the commented line, but not sure what the best approach would be, any ideas? Thanks!

我正在使用 foreach 循环为我正在开发的 WordPress 插件创建导航,但我不想要“|” 要显示最后一个元素,上面的代码是我到目前为止所得到的,我正在考虑在注释行上使用 if 语句,但不确定最好的方法是什么,有什么想法吗?谢谢!

回答by Yeroon

The end()function is what you need:

end()功能是您需要的:

if(end($tabs2) !== $name){
    echo ' |'; // not the last element
}

回答by Waynn Lue

I find it easier to check for first, rather than last. So I'd do it this way instead.

我发现首先检查比最后检查更容易。所以我会这样做。

$first = true;
foreach( $tabs2 as $tab2 => $name ){
    if ($first) {
      $first = false;
    } else {
      echo(' | ');
    }
    $class = ( $tab2 == $current ) ? ' current' : '';
    echo("<li class='posts'><a href='?page=pigg&tab=help&tab2=$tab2' class='$class'>$name</a></li>");
}

I also combined the last two echostogether.

我也把最后两个echos结合在一起。

回答by Shahar

First thing you need to find out what is the last key of the array, and doing so by finding the array length, using the count()function.
Afterwords we gonna create a counter and add +1 on every loop.
If the counter and the last key are equal then it is the last key.

首先,您需要找出数组最后一个键是什么,并通过使用count()函数查找数组长度来实现。
之后我们将创建一个计数器并在每个循环中添加 +1。
如果计数器和最后一个键相等,则它是最后一个键。

    $last = count($array);
    $counter = 1;
    foreach ($array as $key => $val){
    if ($counter != $last){
        // all keys but the last one
        // do something     
       $counter++; // add one to counter count
        }
        else {
            // this is for the last key
    }// end else

}// end foreach

回答by stravanato

I would do this way:

我会这样做:

$arrLi = array();
foreach( $tabs2 as $tab2 => $name ){
  $class = ( $tab2 == $current ) ? ' current' : '';
  $arrLi[] = "<li class='posts'><a href='?page=pigg&tab=help&tab2=$tab2' class='$class'>$name</a></li>";
}
echo implode('|', $arrLi);

回答by Jehad Ahmad Jaghoub

end() is good function to use

end() 是一个很好用的函数

foreach( $tabs2 as $tab2 => $name ){
if(end($tabs2)== $name)
 echo "|";
}

or you can do it manually for more understanding

或者您可以手动执行以获得更多理解

  $copyofarry = $tabs2;
    $last = array_pop($copyofarry);
    foreach( $tabs2 as $tab2 => $name ){
        if($last == $name)
         echo "|";
    }

回答by Dirk Thannh?user

Why not pop the last element first? So you do not need to check if the current element is the last element in each iteration.

为什么不先弹出最后一个元素?所以你不需要在每次迭代中检查当前元素是否是最后一个元素。

The function array_pop(&$array) returns the last element and removes it from the array.

函数 array_pop(&$array) 返回最后一个元素并将其从数组中删除。

<div id="breadcrumb">
    <?php 
        $lastBreadcrumb = array_pop($breadcrumb);
        foreach ($breadcrumb as $crumb){ ?>
            <a href=""><?php echo $crumb; ?></a>
        <?php } ?><span><?php echo $lastBreadcrumb?></span>
</div>

回答by Jovan Perovic

Something like this is possible:

像这样的事情是可能的:

$size = count($tabs2);
$counter = 0;
foreach( $tabs2 as $tab2 => $name ){
    $class = ( $tab2 == $current ) ? ' current' : '';
    echo("<li class='posts'><a href='?page=pigg&tab=help&tab2=$tab2' class='$class'>$name");
    if ( ++$counter < $size ){
        echo(' |'); // If array last then do not display     
    }
    echo('</a></li>');
}