简体   繁体   中英

Looping through Foreach, calling results inside of if statement

How can I return the data which is presented inside of this foreach element, inside of my if statement, which comes after the initial foreach.

foreach ($arr as $line) { 
   print $line;
}

When we print $line it should return something like this.

Example 1
Example 2
Example 3
Example 4

I want to call each element from line, to be set inside of this if statement, but only if the statement contains a specific word.

$status_check = $_GET['status'];
if (strpos($status, 'online') !== false) {
  print $line; // return all of the contents from the (foreach) here.
}

Based on your comments you need to return the data of the array outside of the foreach loop so you dont need to use one.

if (strpos($_GET['status'], 'online') !== false) {
    print array_values($arr);
}

Will return:

Example 1
Example 2
Example 3
Example 4

Only if $_GET['status'] contains online

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM