如何使用while循环进行计数,并在不知道要计数多少元素的情况下防止未定义的偏移


How to count with while loop and prevent undefined offset without knowing how much elements to count?

使用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与我所有的计数参数进行了比较,但没有帮助。有可能以某种方式防止未定义的偏移吗?对不起,我不能发布整个代码。

有什么想法吗?

while (isset($page_counter[$rrr]) && endWith($page_counter[$rrr], '.jpg')) ...