2016-01-22 99 views
-2

我想在登錄菜單後刪除最後一個分隔符。 這是我的代碼和輸出。刪除最後一個菜單的分隔符

   <?php 
       if (!empty($topmenu) && !empty($menulist)) { 
        foreach ($topmenu as $mainparent) { 
         $arry = getmenuvalue($mainparent->id, $menulist, MAINURL); 
         if (isset($mainparent->children) && !empty($mainparent->children)) { 
          echo '<li class="dropdown"> <a href="' . $arry['url'] . '" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">' . $arry['name'] . '<span class="caret"> </span></a>'; 

          echo '</li>'; 
          echo '<li> | </li>'; 
         } else { 
          echo '<li><a href="' . $arry['url'] . '">' . $arry['name'] . '</a></li>'; 
          echo '<li> | </li>'; 
         } 
        } 
       } 
       ?> 

結果這個代碼是

Home | Register | Login | 

我想登錄菜單之後刪除最後的分隔。 我想要這樣的結果。

Home | Register | Login 
+0

使用數組長度得到最後一個元素和wirte代碼('echo'

  • |
  • ';')在條件 –

    +0

    如果它不是數組的最後一個元素,則打印回顯'

  • |
  • '; else''http://stackoverflow.com/questions/14701286/php-remove-comma-from-the-last-loop –

    +0

    給我代碼我是學生 –

    回答

    1

    嘗試這個

    <?php 
           if (!empty($topmenu) && !empty($menulist)) { 
            $count = count($topmenu); 
            $i = 1; 
            foreach ($topmenu as $mainparent) { 
    
             $arry = getmenuvalue($mainparent->id, $menulist, MAINURL); 
             if (isset($mainparent->children) && !empty($mainparent->children)) { 
    
              echo '<li class="dropdown"> <a href="' . $arry['url'] . '" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">' . $arry['name'] . '<span class="caret"> </span></a>'; 
    
              echo '</li>'; 
              if($count != $i) 
              echo '<li> | </li>'; 
             } else { 
              echo '<li><a href="' . $arry['url'] . '">' . $arry['name'] . '</a></li>'; 
              if($count != $i) 
              echo '<li> | </li>'; 
             } 
            $i++; 
            } 
           } 
           ?> 
    
    1

    你可以按照這個例子:

    <? 
    $array = array('One','Two','Three'); // your array 
    
    $count = count($array); // check the array count 
    $i = 1; // use incremental 
    foreach ($array as $value) { 
        $separator = ($i == $count ? '' : '|'); // compare if last index use empty else separator 
        echo $value. $separator; // print separator with value 
        $i++; // +1 in every iteration. 
    } 
    ?> 
    

    結果:

    One|Two|Three 
    

    更新1:

    與示例代碼

    <? 
    if (!empty($topmenu) && !empty($menulist)) { 
    
        $count = count($topmenu); 
        $i = 1; 
        foreach ($topmenu as $mainparent) { 
         $arry = getmenuvalue($mainparent->id, $menulist, MAINURL); 
         if (isset($mainparent->children) && !empty($mainparent->children)) { 
          echo '<li class="dropdown"> <a href="' . $arry['url'] . '" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">' . $arry['name'] . '<span class="caret"> </span></a>'; 
    
          echo '</li>';    
         } else { 
          echo '<li><a href="' . $arry['url'] . '">' . $arry['name'] . '</a></li>'; 
          //echo '<li> | </li>'; 
         } 
         if($i == $count ? '' : '<li> | </li>'); 
         $i++; 
        } 
    } 
    ?> 
    

    側面說明:

    我不知道有關$topmenu檢查count($topmenu);,如果你得到它的工作計數。嘗試一下。

    +0

    將此代碼集成到我的代碼中請@devpro –

    +0

    @KevinPatel::)檢查您的代碼。 – devpro

    +0

    @KevinPatel:代碼已更新,請嘗試 – devpro

    0

    你可以收集你的列表項和CONCAT他們使用implode方法:

    <?php 
        if (!empty($topmenu) && !empty($menulist)) { 
    
         $listItems = array(); 
         foreach ($topmenu as $mainparent) { 
          $arry = getmenuvalue($mainparent->id, $menulist, MAINURL); 
          if (isset($mainparent->children) && !empty($mainparent->children)) { 
           $listItems[] = '<li class="dropdown"> <a href="' . $arry['url'] . '" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">' . $arry['name'] . '<span class="caret"> </span></a></li>'; 
          } else { 
           $listItems[] = '<li><a href="' . $arry['url'] . '">' . $arry['name'] . '</a></li>'; 
          } 
         } 
    
         echo implode('<li> | </li>', $listItems); 
        } 
    ?> 
    
    0

    你可以做這樣的事情

    <?php 
        if (!empty($topmenu) && !empty($menulist)) { 
         foreach ($topmenu as $key => $mainparent) { 
          $arry = getmenuvalue($mainparent->id, $menulist, MAINURL); 
          if (isset($mainparent->children) && !empty($mainparent->children)) { 
           echo '<li class="dropdown"> <a href="' . $arry['url'] . '" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">' . $arry['name'] . '<span class="caret"> </span></a>'; 
    
           echo '</li>'; 
           //echo '<li> | </li>'; 
          } else { 
           echo '<li><a href="' . $arry['url'] . '">' . $arry['name'] . '</a></li>'; 
           //echo '<li> | </li>'; 
          } 
    
          end($topmenu); 
          if ($key !== key($topmenu)) { 
           echo '<li> | </li>'; 
          } 
    
         } 
        } 
    ?>