【发布时间】:2023-04-02 04:15:01
【问题描述】:
有没有办法在 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 as $theme where $theme['section'] == 'headcontent')
这个可以吗
【问题讨论】:
-
您可以在 foreach 循环之前过滤数组,因此首先过滤“headcontent”,然后循环。但我认为你不会从中获得任何好处。
-
你每次都运行两个循环吗?我们在那些
if-statements 中谈论了多少代码?两种情况下的内容是相似还是完全不同? -
我不明白,当你已经达到一定的价值时,你会追求什么