3

In this case I only use php to do 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  

'FA line 1' line from the 'FileA.txt' is matched with various lines of the 'FileB.txt' the way I know till which line is from the 'FA line 1' is that is separated with this '-'.

In the first example, 3 lines of 'FileB.txt' match one line of 'FileA.txt', but could be the case that 'FA line 1' match 3 lines, 'FA line 2' match 4 lines and 'FA line 3' match 2 lines like this:

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 could be separated with some other symbol like this one '_' for example, but is important that is separated to know how much lines are from each.

The goal is to put those 2 files in an associative array this way without the '-' because is only a separator (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 would be 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 tryed many times with different ways but couldn't achieve this.

This are some failed attempts that gave me different kind of 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 to solve this?

Thanks in advance for the help! :D

LF00
  • 27,015
  • 29
  • 156
  • 295

1 Answers1

1

Try this, refer 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));
Community
  • 1
  • 1
LF00
  • 27,015
  • 29
  • 156
  • 295