在WHILE循环条件

时间:2015-10-15 05:34:51

标签: php arrays while-loop conditional

我有这段代码

$limit = 100;
$offset = 0;

while (array() === ($contactIdList = $this->getData($limit, $offset))) {
    $count = count($contactIdList);
    //using $count and $contactIdList for doing some logging stuff  
    $offset += $limit;
}
  

$这 - >的getData($偏移)   //从我们设置$ limit,$ offset的查询中返回结果数组,这样当获取所有结果时,它返回空数组,而条件结束则返回此数据。

我的问题是我在$contactIdList条件中分配数组结果,条件是在条件下保存一行$contactIdList = $this->getData($limit, $offset)。这是正确的方法吗?

2 个答案:

答案 0 :(得分:1)

这对我来说很好。只有一个建议,如果您使用PHP 5.5.0及更高版本来检查空数组

,则可以使用empty()
$limit = 100;
$offset = 0;

while(!empty($contactIdList = $this->getData($limit, $offset))) {
    $count = count($contactIdList);
    //using $count and $contactIdList for doing some logging stuff  
    $offset += $limit;
}

答案 1 :(得分:0)

你可以简单地做 -

while ($contactIdList = $this->getData($limit, $offset)) {
相关问题