我有一个函数返回mp3列表的质量。我想要返回HD值如果我到达数组中的最后一个元素,但我在foreach循环中遇到问题,则返回true。它总是返回true。如何在foreach循环中获得最高数字
代码
function funcName() {
foreach($dirs as $d) {
if (filesize($d) > 200) {
$qualities = substr(strrchr(basename($d), "-"), 1);
$qualities = preg_replace('/\\.[^.\\s]{3,4}$/', '', $qualities);
// This is where I check whether it is the last element or not.
$numItems = count($dirs);
$i = 0;
foreach($dirs as $key => $value) {
if (++$i === $numItems) {
$zaa = true;
} else {
$zaa = false;
}
}
$files[] = ["files" => basename($d), "qualities" => $qualities, "hd" => $zaa];
}
}
return ($files);
}
我只是想回到$zaa = true
如果我达到了最后一个元素。在我的代码中,它始终返回true。
请问你能告诉我我失败的部分吗?
将您的计数($ dirs)放在您的foreach循环之上,每迭代一次,您将重新计算数组/集合的大小。这是表现不是解决方案。 –