如何使用break in if(Command)

时间:2016-06-17 19:44:14

标签: php

我在foreach循环中使用 $ i 字符串作为限制。

$i = 0;
foreach($string as $menu){
   $i++;
   if($i == 6){
   break;
   }
   $menu_name = $menu['name'];
   $menu_style = $menu['style'];

   // i want to show total 5 menu names.


   if($menu_style == 'magazine'){
   // i have 5+ menus names (magazine style)
   // butt here i want to show just one last magazine 
   echo $menu_name;
   }

   if($menu_style == 'normal'){
   // i have 5+ names menus (normal style)
   // butt here i want to show  4 last normal style 
   echo $menu_name.'<br>';
   }

}

我不想在SQL查询中使用LIMIT。

  

当我使用 if($ i == 5){break; } 然后代码显示只有4   菜单名称

告诉我如何显示菜单名称作为我的要求。

1 个答案:

答案 0 :(得分:0)

那样的东西?

$i = 0;

foreach($string as $menu){
   $i++;
   if($i == 6){
   break;
   }
   $menu_name = $menu['name'];
   $menu_style = $menu['style'];

   // i want to show total 5 menu names.


   if($menu_style == 'magazine' && $i <= 5){
   // i have 5+ menus names (magazine style)
   // butt here i want to show just one last magazine 
       echo $menu_name;
   } else if($menu_style == 'normal' && $i <= 4){
   // i have 5+ names menus (normal style)
   // butt here i want to show  4 last normal style 
        echo $menu_name.'<br>';
   }

}