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

在PHP中使用嵌套的while循环从文件中获取值,php,arrays,file,Php,Arrays,File,我的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(';',

我的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.

}

我在代码中注释时遇到的问题。任何人都知道,请帮助我,谢谢。

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

的while循环中,非常感谢您的回答,但是当我输出它时,会显示
ArrayArray
Yes$data2Array将是一个包含$data2元素的数组。因此,试试这个print\r($data2Array),您将得到结构。您可以像echo$data2Array[0][0]那样进行回显
$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.

}