Php 循环通过Foreach,在if语句内部调用结果
如何返回该Php 循环通过Foreach,在if语句内部调用结果,php,Php,如何返回该foreach元素中的数据,该元素位于初始foreach之后的if语句中 foreach ($arr as $line) { print $line; } 当我们打印$line时,它应该返回如下内容 Example 1 Example 2 Example 3 Example 4 我想从第行调用每个元素,在这个if语句中设置,但仅当该语句包含特定单词时 $status_check = $_GET['status']; if (strpos($status, 'online')
foreach
元素中的数据,该元素位于初始foreach之后的if
语句中
foreach ($arr as $line) {
print $line;
}
当我们打印$line
时,它应该返回如下内容
Example 1
Example 2
Example 3
Example 4
我想从第行调用每个元素,在这个if
语句中设置,但仅当该语句包含特定单词时
$status_check = $_GET['status'];
if (strpos($status, 'online') !== false) {
print $line; // return all of the contents from the (foreach) here.
}
根据您的注释,您需要返回foreach循环外部的数组数据,因此不需要使用
if (strpos($_GET['status'], 'online') !== false) {
print array_values($arr);
}
将返回:
Example 1
Example 2
Example 3
Example 4
仅当
$\u GET['status']
包含联机
时,您的意思是将if
语句复制到循环中吗?因此,如果$\u GET['status'],您只想返回行(例如示例1、示例2等)
containsonline
?@OliGirling我想返回if
语句中的那些行,当$\u GET['status']
containsonline
问题是,我这样做是为了创建WordPress帖子,所以我必须在初始foreach
循环之外加载if
语句。