John Siniger
John Siniger

Reputation: 885

How to remove row from Mysql Query result on foreach depending value of the same array

Hello I am using the following code to display array information

$select = new Class;
$stmt = $select->selecttimemonthlyall();
foreach($stmt as $row){

echo $row['name'];
echo $row['time'];

} 

Where $stmt is the execution of the query PDO object

Question here is how I can limit the results depending the the $row['time'] for example

I have a variable $key = 30;

then I need to put a condition like this

if(($key - $row['time']) < 30) { 
    unset($row[current]); 
}

Any help is welcome. Thanks.

Upvotes: 1

Views: 905

Answers (2)

Ettore Delprino
Ettore Delprino

Reputation: 11

You can unset the element of the array with the key of the foreach:

$stmt = $select->selecttimemonthlyall();

foreach($stmt as $k => $row) {
  if(($key - $row['time']) < 30) {
    unset($smt[$k]); 
  }
}

Upvotes: 1

Fabio
Fabio

Reputation: 23510

You can just not display in your loop according to your condition

$key = 30;
foreach($stmt as $row){
    if(($key - $row['time']) >= $key) { 
       echo $row['name'];
       echo $row['time'];
    }
}

I would rather limit returned rows directly in the query instead of filtering them in the loop with lack of resources

Upvotes: 0

Related Questions