有没有办法在PHP中为foreach方程添加where类.
目前我正在向这样的foreach添加一个if.
<?php foreach($themes as $theme){
if($theme['section'] == 'headcontent'){
//Something
}
}?>
<?php foreach($themes as $theme){
if($theme['section'] == 'main content'){
//Something
}
}?>
据推测,PHP必须遍历每个结果的所有结果.有没有更有效的方法来做到这一点.就像是
foreach($themes为$theme,其中$theme [‘section’] ==’headcontent’)
可以做到这一点
解决方法:
使用SWITCH语句.
<?php
foreach($themes as $theme)
{
switch($theme['section'])
{
case 'headcontent':
//do something
break;
case 'main content':
//do something
break;
}
}
?>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。