如何在while循环中比较数组中的值?

时间:2021-01-30 18:58:56

My while loop condition looks like this:

while循环条件是这样的:

while ($currentmodif <= $lastmodif) 
{
.....thing to be done
}

Now instead of comparing between two values, I compare between array and a string.

现在我不是比较两个值,而是比较数组和字符串。

The array look like this:

数组是这样的:

array(2) {
  [0]=>
  array(2) {
    ["time"]=>
    int(1473735528)
    ["id"]=>
    string(1) "3"
  }
  [1]=>
  array(2) {
    ["time"]=>
    int(1473507326)
    ["id"]=>
    string(1) "4"
  }
}

And

$lastmodif = 1473503210;

So how do I compare if any value of key called time in the given array is greater than $lastmodif in while loop?

那么,我如何比较给定数组中调用时间的键值大于$lastmodif的while循环呢?

1 个解决方案

#1


0  

To expand on @siddhesh's comment, foreach will loop through your array just fine.

要扩展@悉达什的评论,foreach将会对你的数组进行循环。

foreach($yourarray as $item) {
  if($item['time'] <= $lastmodif) {
     // do your work
  }
}

This loop goes through your array and giving you a copy of each item. If you want to modify the items, you need to use the reference:

这个循环遍历您的数组,并给您每个条目的副本。如果您想修改项目,您需要使用参考:

foreach($yourarray as &$item) {      // <-- notice the &
  if($item['time'] <= $lastmodif) {
    // do your work
  }
}

#1


0  

To expand on @siddhesh's comment, foreach will loop through your array just fine.

要扩展@悉达什的评论,foreach将会对你的数组进行循环。

foreach($yourarray as $item) {
  if($item['time'] <= $lastmodif) {
     // do your work
  }
}

This loop goes through your array and giving you a copy of each item. If you want to modify the items, you need to use the reference:

这个循环遍历您的数组,并给您每个条目的副本。如果您想修改项目,您需要使用参考:

foreach($yourarray as &$item) {      // <-- notice the &
  if($item['time'] <= $lastmodif) {
    // do your work
  }
}