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!

2
  • You can move the 2nd and 3rd echo above the first one. Then if (iteration== first_time) "do_not_echo_2nd,3rd". Commented Mar 14, 2012 at 10:30
  • Take a look at this solution: stackoverflow.com/a/29474468/1478566 Commented Apr 6, 2015 at 15:41

7 Answers 7

64

The end() function is what you need:

if(end($tabs2) !== $name){
    echo ' |'; // not the last element
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, this worked perfectly, I'll accept this as the answer, but I got to wait 3 minutes. xD
@KristianMatthews, you nearly waited 2 years now btw. :)
Minor tweak, I would cache end($tabs2) in a var before the foreach loop so that the end function isn't run every time.
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 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.
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.

2 Comments

Thanks for the help, but this just immediately sets $first = true; then $first = false; essentially.
Hm, that's surprising -- it doesn't only echo out the break at the beginning of each non-first line?
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

Comments

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

Comments

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

Comments

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

Comments

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.