21
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!

Kristian Matthews
  • 800
  • 3
  • 14
  • 28

7 Answers7

64

The end() function is what you need:

if(end($tabs2) !== $name){
    echo ' |'; // not the last element
}
Yeroon
  • 3,223
  • 2
  • 22
  • 29
  • Thank you, this worked perfectly, I'll accept this as the answer, but I got to wait 3 minutes. xD – Kristian Matthews Mar 14 '12 at 10:34
  • 23
    @KristianMatthews, you nearly waited 2 years now btw. :) – thasmo Jan 27 '14 at 19:40
  • 5
    Minor tweak, I would cache end($tabs2) in a var before the foreach loop so that the end function isn't run every time. – terraling Oct 09 '14 at 09:13
  • 4
    What if we got two equal values in the array, 1 in the end and the other in the middle? Wouldn't this be quite misleading.. – mikewasmike Jun 12 '15 at 07:55
  • @mikewasmike you could always change the if to be if((end($tab2) !== $tab2) && (end($name) !== $name)). This would only ever trigger the second value if the first is true, which isn't too bad. – Jovin J Jun 18 '16 at 21:13
1

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.


    $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

Shahar
  • 2,101
  • 18
  • 23
1

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);
stravanato
  • 146
  • 6
1

end() is good function to use

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 "|";
    }
Jehad Ahmad Jaghoub
  • 1,225
  • 14
  • 22
1

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.

Waynn Lue
  • 11,344
  • 8
  • 51
  • 76
0

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.

<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>
0

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>');
}
Jovan Perovic
  • 19,846
  • 5
  • 44
  • 85