[英]PHP recursive function not allowing page to load
我试图在不包含另一个文件的路径以及文件名的文件的每一行中显示总数,但是如果它有另一个文件的路径,则循环遍历该文件并将其所有数字相加,并且只要当前文件中存在指向文件的路径,循环就会继续进行。
这是我的代码
function fileProcessor($value){
if(file_exists(trim($value))){
$total = 0;
$files = file($value, FILE_SKIP_EMPTY_LINES);
foreach($files as $data) {
if(!preg_match("/.txt/i", $data)){
$num = floatval($data);
$total += $num;
} else {
fileProcessor(trim($data));
}
}
echo $value. ' - ' .($total);
} else {
echo 'File does not exist';
}
fileProcessor('text_files/first.txt');
}
我正在使用 3.txt 文件,在这些文件中我有这样的东西
第一个.txt
第二个.txt
第三个.txt
我正在寻找的 output
如果有人能指出我正确的方向,我将不胜感激,我不知道我是否做得对。
您的代码有两个问题:
更正这些问题,并将变量重命名为有意义的东西会给出以下代码:
function fileProcessor($filename){
if(file_exists(trim($filename))){
$total = 0;
$rows = file($filename, FILE_SKIP_EMPTY_LINES);
foreach($rows as $data) {
if(!preg_match("/.txt/i", $data)){
$num = floatval($data);
$total += $num;
}else {
$total += fileProcessor(dirname($filename)."/".trim($data));
}
}
echo $filename. ' - ' .($total)."<br>\n";
}else{
echo "File does not exist<br>\n";
}
return $total;
}
fileProcessor('text_files/first.txt');
Output:
text_files/third.txt - 3
text_files/second.txt - 8
text_files/first.txt - 15
这会按照总计最终累积的顺序列出文件,因此最低级别首先出现。
[编辑] 如果文件中出现两个或多个文件名,我发现结果顺序存在问题。 这是一个处理这个问题的重做版本。
要按照遇到的顺序列出文件,需要颠倒自然顺序。 在下面的新版本中,我将文件名放在$fileList
数组中,该数组通过引用传递。 function 的每次新调用都会将其结果添加到该数组的末尾。 处理完成后,将显示阵列。
function fileProcessor( &$fileList){
// Get the last filename in the array
end($fileList);
$filename = key($fileList);
if(file_exists(trim($filename))){
// Accumulate the values
$fileList[$filename] = 0;
$rows = file($filename, FILE_SKIP_EMPTY_LINES);
foreach($rows as $data) {
if(!preg_match("/.txt/i", $data)){
$num = floatval($data);
$fileList[$filename] += $num;
}else {
// Recursive call. Add the next filename to the array
$fileList[dirname($filename)."/".trim($data)]=0;
$fileList[$filename] += fileProcessor($fileList);
}
}
} else {
$fileList[$filename]= "File does not exist: $filename";
}
// return the total for the file to add to the accumulator for the file above.
return $fileList[$filename];
}
// Place the initial file in the array
$results = ['text_files/first.txt' => 0];
// run the function
fileProcessor($results);
// display the results
foreach($results as $filename=>$total) {
echo $filename.' - '.$total."<br>\n";
}
Output:
text_files/first.txt - 15
text_files/second.txt - 8
text_files/third.txt - 3
您可以使用 static var:
<?php
function fileProcessor($value) {
static $results = [];
if (file_exists(trim($value))) {
$results[$value] = 0;
$files = file($value, FILE_SKIP_EMPTY_LINES);
foreach($files as $data) {
if (!preg_match("/.txt/i", $data)) {
$num = floatval($data);
$results[$value] += $num;
} else {
fileProcessor(trim($data));
}
}
} else {
echo 'File does not exist';
}
reset($results);
if (key($results) != $value) {
return;
}
foreach($results as $key => $value) {
echo $key. ' - ' .$value."\n";
}
}
fileProcessor('text_files/first.txt');
Output:
text_files/first.txt - 7
text_files/second.txt - 5
text_files/third.txt - 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.