-6

I have an archive txt in this format.

$line[0] = 50009828720001007029552330034 20181009MG551 0119195102P000000002624400000000000000000000000000000000000000000000000262440000N

In this line I can get many information of a determinated document.

Example: When I use the functions below I can get the number of document, date of document, and another information.

$numDoc[0] = (string)substr($line[0], 46, 5); // THE NUMBER OF DOCUMENT IS 011919
$dateDoc[0] = (string)substr($line[0], 30, 8); // DATE OF DOCUMENT IS 20181009

How I can structure my archive to easy print the information ordered like this.

NUMBER OF DOCUMENT - DATE DOCUMENT

What the easy way to ordered this information, is a small application does not allow user make anything, just upload this archive .txt.

My question is about structure the informations for easy acess, like bellow:

doc1{
number = xxx
date = xxx
generic = xxx
}

1 Answers1

-1

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);
}
Andreas
  • 23,610
  • 6
  • 30
  • 62
  • I have many lines to `substr()`, this code will create many arrays? I'm thinking to make an object system, one class for the archive, and another class inside the archive named "doc", and in doc many properties extratec from line. But i dont have idea how to make this. – Felipe Guimarães Nov 14 '18 at 12:29
  • And I have no idea what you are looking for. – Andreas Nov 14 '18 at 12:30
  • @FelipeGuimarães here is what the output is of above code: https://3v4l.org/psGtn – Andreas Nov 14 '18 at 12:35