PHP递归函数不允许加载页面



我试图在不包含另一个文件路径和文件名的文件的每一行中显示总数,但如果它有一个指向另一个文件的路径,则循环通过该文件并求和它所拥有的所有数字,并且只要当前文件中存在一个指向文件的路径就一直循环下去。

这是我的代码

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文件正在处理中,在这些文件中我有类似于的东西

first.txt


  • 1
  • 3
  • 3
  • second.txt

second.txt


  • 2
  • 3
  • 第三个.txt

third.txt


  • 1
  • 2

我正在寻找的输出


  • first.txt-15
  • second.txt-8
  • third.txt-3

如果有人能给我指明正确的方向,我会非常感激,我不知道我做得是否正确。

您的代码有两个问题:

  • 在子文件的路径中没有包含源文件的目录,因此永远找不到这些文件
  • 您没有从函数返回总数,以便更高级别的调用可以添加子文件的总数

纠正这些问题,并将变量重命名为有意义的东西,得到了以下代码:

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');

输出:

text_files/third.txt - 3
text_files/second.txt - 8
text_files/first.txt - 15

这将按最终累积总数的顺序列出文件,因此最低级别将首先出现。

[编辑]如果一个文件中出现两个或多个文件名,我发现结果的顺序有问题。这里有一个重新设计的版本来处理这个问题。

要按遇到文件的顺序列出文件,需要颠倒自然顺序。在下面的新版本中,我将文件名放置在$fileList数组中,该数组通过引用传递。函数的每次新调用都会将其结果添加到该数组的末尾。处理完成后,将显示阵列。

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";
}

输出:

text_files/first.txt - 15
text_files/second.txt - 8
text_files/third.txt - 3

您可以使用静态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');

输出:

text_files/first.txt -  7
text_files/second.txt -  5
text_files/third.txt -  3

最新更新