Reading 2 files into an associative array

In this case, I only use php for this.
I have these .txt files to read:

File A

FA line 1  
FA line 2  
FA line 3  

      

File B

FB line 1  
FB line 2  
FB line 3  
-  
FB line 4  
FB line 5  
FB line 6  
-  
FB line 7  
FB line 8  
FB line 9  

      

'line line 1' from FileA.txt is matched against different lines of "FileB.txt", as I know to which line the line "FA line 1" is, which is separated by this "-".

In the first example, three lines "FileB.txt" correspond to one line "FileA.txt", but it could be that "FA line 1" corresponds to 3 lines, "FA line 2" corresponds to 4 lines and "FA line 3" corresponds to two lines:

File A

    FA line 1  
    FA line 2  
    FA line 3  

      

File B

    FB line 1  
    FB line 2  
    FB line 3  
    -  
    FB line 4  
    FB line 5  
    FB line 6  
    FB line 7  
    -
    FB line 8  
    FB line 9  

      

It may be separated by some other character like this, for example "_", but it is important that you separate it to find out how many lines there are from each.

The goal is to put these 2 files in an associative array this way without the '-', because this is only a delimiter (first example):

$ArrayResult[FA line 1] = "FB line 1  
                           FB line 2  
                           FB line 3";

$ArrayResult[FA line 2] = "FB line 4  
                           FB line 5  
                           FB line 6";

$ArrayResult[FA line 3] = "FB line 7  
                           FB line 8  
                           FB line 9";

      

or in the second example it will look like this:

$ArrayResult[FA line 1] = "FB line 1  
                           FB line 2  
                           FB line 3";

$ArrayResult[FA line 2] = "FB line 4  
                           FB line 5  
                           FB line 6
                           FB line 7";

$ArrayResult[FA line 3] = "FB line 8  
                           FB line 9";

      

I tried many times in different ways but couldn't get it.

These are some failed attempts that gave me different errors or didn't work, also I don't know which way is more efficient:

First way:

        $rute1 = "FileA.txt";
        $rute2 = "FileB.txt";

        if(is_file($rute1) && is_file($rute2)){

            if(($fh1 = fopen($rute1, "r")) && ($fh2 = fopen($rute2, "r"))){

                $result = array();
                $aux1 = array();
                $aux2 = array();

                $line1="";
                while (!feof($fh1)){
                    $line1 = fgets($fh1);
                    array_push($aux1, $line1);
                }

                $linea2="";
                while (!feof($fh2)){

                        if(fgets($fh2)===("-")){
                            array_push($aux2, $line2);
                            $line2="";
                        } else {
                            $line2 .= fgets($fh2);
                        }

                }

            }
        }

fclose($fh1);
fclose($fh2);


    for ($i=0;$i=sizeof($aux1);$i++) {
        $Result[$aux1[$i]]=$aux2[$i];
    }


    print_r($result);

      

Second way:

$rute1 = "FileA.txt";
$rute2 = "FileB.txt";

if(is_file($rute1) && is_file($rute2)){

    if(($fh1 = fopen($rute1, "r")) && ($fh2 = fopen($rute2, "r"))){

        $result = array();
        $pointer = 0;
        $line1="";

        while (!feof($fh1)){
            $line1 = fgets($fh1);
            $line2="";

            while (!feof($fh2)){
                fseek($fh2, $pointer);

                if(fgets($fh2)==="-"){
                    $pointer = ftell($fh2);
                    break;
                } else {
                    $line2 .= fgets($fh2);
                }

            }

            $result[$line1]=$line2;

        }





    }

    fclose($fh1);
    fclose($fh2);
}


print_r($Result);

      

Any ideas for solving this problem?

Thanks in advance for your help!: D

+3


source to share


1 answer


Try this, refer to this for file usage.



$keys = file($fileA, FILE_IGNORE_NEW_LINES);
$values = array_map(function($v){return trim($v);},explode('-', file_get_contents($fileB)));
print_r(array_combine($keys, $values));

      

+1


source







All Articles