[Solved] How to read a file line by line in PHP


I assume you need a way to make it more dynamic than manually typing [0] then [1] and so on?

You can loop the txtfile with foreach and assign new array items with [].

$file = fopen("path of txt");

foreach($file as $line){
    $numDoc[] = substr($line, 46, 5); 
    $dateDoc[] = substr($line, 30, 8);
}

The code above will loop through the document and create two arrays with the dates and the document number.

I would probably make it an multidimensional array instead to keep all in one place.

foreach($file as $line){
    $array["numdoc"][] = substr($line, 46, 5); 
    $array["dateDoc"][] = substr($line, 30, 8);
}

3

solved How to read a file line by line in PHP