在PHP中使用嵌套while循环从文件中获取值



我的PHP代码如下:

$file1 = 'file1.txt';
$file2 = 'file2.txt';
$files1 = fopen($file1,'r');
$files2 = fopen($file2,'r');
$title = "";    
while(!feof($files1)){
$data1 = explode(';', fgets($files1));
    while(!feof($files2)){
        $data2 = explode(';', fgets($files2));      
    }
    //I want to output $data2[0] in here
    //and want to make the calculation between $data1[0] - $data2[0]
    // The problem is that I can not use the $data1[0] outside the while loop of file2 that make me can not calculation.
}

我在代码中注释的问题。谁知道,请帮助我,谢谢。

$file1 = 'file1.txt';
$file2 = 'file2.txt';
$files1 = fopen($file1,'r');
$files2 = fopen($file2,'r');
$title = "";    
while(!feof($files1)){
$data1 = explode(';', fgets($files1));
    while(!feof($files2)){
        $data2 = explode(';', fgets($files2));      
        $data2Array[]=$data2;
    }
     // You can use $data2Array[0] here now
    //I want to output $data2[0] in here
    //and want to make the calculation between $data1[0] - $data2[0]
    // The problem is that I can not use the $data1[0] outside the while loop of file2 that make me can not calculation.
}

由于while循环是嵌套的,您仍然可以在创建$data2的while循环中使用$data1,因为它也在您定义$data1的while循环中

相关内容

  • 没有找到相关文章

最新更新