简体   繁体   English

PHP Foreach如果数组最后

[英]PHP Foreach If Array Last

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 ' |' 我正在使用foreach循环为我正在处理的WordPress插件创建导航,但我不想要'|' 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? 为了显示最后一个元素,上面的代码是我到目前为止所做的,我想在注释行上使用if语句,但不确定最好的方法是什么,任何想法? Thanks! 谢谢!

The end() function is what you need: end()函数是你需要的:

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

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. 首先需要找出数组最后一个键 ,并使用count()函数找到数组长度。
Afterwords we gonna create a counter and add +1 on every loop. 后来我们要创建一个计数器并在每个循环上添加+1。
If the counter and the last key are equal then it is the last key. 如果计数器和最后一个键相等,则它是最后一个键。

$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

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);

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 "|";
    }

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 echos together. 我还将最后两个echos合并在一起。

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>

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>');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM