簡體   English   中英

遍歷Foreach,在if語句內部調用結果

[英]Looping through Foreach, calling results inside of if statement

我該如何返回在此foreach元素內部,在我的if語句內部,位於初始foreach之后的數據。

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

僅當$_GET['status']包含online

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM