简体   繁体   中英

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

The end() function is what you need:

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

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

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.

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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