【问题标题】:How to count with while loop and prevent undefined offset without knowing how much elements to count?如何在不知道要计算多少元素的情况下使用 while 循环计数并防止未定义的偏移量?
【发布时间】:2013-12-13 08:20:02
【问题描述】:

当使用 while 循环计数时(在“for 循环”内),我使用的不是长度参数,而是这个函数:

function endsWith($haystack, $needle)
{
    return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
}

借助它,我循环浏览了一组预览缩略图和 pdf 源文件。但我只想计算以jpg结尾的文件。所以我的while循环代码是:

while(endWith($page_counter[$rrr], '.jpg')){ 
                $rrr++;
                $document_page_count++;                                       
                $page_counter['pdf'][$pdf_counting-1]=$document_page_count;
            }

我总是收到通知:“未定义的偏移量:10”。我已经用“isset”尝试了一些东西,并将 $rrr 与我拥有的所有计数参数进行了比较,但它没有帮助。是否有可能以某种方式防止未定义的偏移量?抱歉,我无法发布整个代码。

有什么想法吗?

【问题讨论】:

  • isset() 应该可以完成这项工作,因此您的代码可能不正确。但由于我们看不到它,我们无法为您提供帮助。
  • while(endWith($page_counter[$rrr], '.jpg') && isset($page_counter[$rrr+1])){ $rrr++; $document_page_count++;我试过了,没有任何警告了。也许这就是答案 $page_counter['pdf'][$pdf_counting-1]=$document_page_count+1; //var_dump($document_page_count); }

标签: php loops while-loop counting


【解决方案1】:
while (isset($page_counter[$rrr]) && endWith($page_counter[$rrr], '.jpg')) ...

【讨论】:

  • 我试过了,但不幸的是它没有用。但是有: while(endWith($page_counter[$rrr], '.jpg') && isset($page_counter[$rrr+1])){ $rrr++; $document_page_count++;我试过了,没有任何警告了。也许这就是答案 $page_counter['pdf'][$pdf_counting-1]=$document_page_count+1;它现在似乎工作了
猜你喜欢
  • 2015-10-12
  • 2015-07-23
  • 2011-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-19
  • 2020-08-10
  • 1970-01-01
相关资源
最近更新 更多