调试看起来像是一个简单的问题,我担心这个模块可能有问题,我忽略了它的简单性:解析PHP数组时遇到不正确的重复问题
function sendUploads(array $files)
{
$fileCount = count($files)-1;
$lastFile = false;
foreach ($files as $fileKey => $file) {
if ($fileKey == $fileCount) {
$file = $file . ' is the lastfile';
}
$uploads[] = $file;
}
var_dump($uploads);
}
$files = array('file1', 'file2', 'file3', 'file4', 'file5');
sendUploads($files);
预期的结果将是:
array(5) {
[0] =>
string(5) "file1"
[1] =>
string(5) "file2"
[2] =>
string(5) "file3"
[3] =>
string(5) "file4"
[4] =>
string(21) "file5 is the lastfile"
}
但在某些边缘情况,我们看到这样的结果,而不是:
array(5) {
[0] =>
string(5) "file1"
[1] =>
string(5) "file2"
[2] =>
string(5) "file3"
[3] =>
string(5) "file4"
[4] =>
string(21) "file5 is the lastfile"
[5] =>
string(21) "file5 is the lastfile"
}
在我所有的测试中,我都得到了第一个结果,但是有一个潜在的情况我错过了这个函数可能导致这个问题的地方?
谢谢你的时间。
第二个例子是不可能的,你发布的代码。请粘贴完整或实际的代码。 – Daan
“在一些边缘情况下”这些情况具体是什么? –
根本看不到这是可能的。 foreach()不会因为它的头发日不好而神奇地重复一次额外的时间。 –