0

I need to parse this .txt database which is not delimited, it's by position. Each field has a start position, end position and length.

I'm going to INSERT this into a MySQL TABLE, but I have to create the layout and working how this data should be defined as an array or as Objects?

Here is a link to the record layout: http://www.state.nj.us/treasury/taxation/lpt/MODIV-Counties/2013/MODIVLayout.pdf

000
  • 26,951
  • 10
  • 71
  • 101
Edward
  • 9,430
  • 19
  • 48
  • 71
  • 3
    [Which do _you_ want to use?](http://stackoverflow.com/questions/2193049/php-objects-vs-arrays) – George Cummins Jun 25 '13 at 21:23
  • Both. An Array of Objects, in my opinion. :) – christopher Jun 25 '13 at 21:24
  • this is up to you. either one would work, but if you can use some kind of parsing module, it will probably read the data into an object of some kind. if you're just going to be inserting it into a database immediately then it doesn't really matter how you temporarily store the data in php. – user428517 Jun 25 '13 at 21:25
  • not sure you need to even consider it. –  Jun 25 '13 at 21:25
  • @George Cummins Whichever is the more modern and elegant way to do this. – Edward Jun 25 '13 at 21:38
  • 1
    @Edward Objects in PHP are newer than arrays, so if that is what you mean by modern, objects are the answer. However, arrays are perfectly useful in this situation (_newer_ is not necessarily _better_). It is really a matter of opinion and familiarity: you should use the one that feels best to you. There is no concrete reason to recommend on over the other in this situation. – George Cummins Jun 25 '13 at 21:40
  • the space shuttle is 'more modern' than the car, but getting across town in it is a bitch. –  Jun 25 '13 at 21:42

1 Answers1

1

The record will be a string, 700 characters long. You will have an array of these strings. Each string, you should convert into an object or hash.

$record_strings = array(
    'DATADATADATADATA...DATADATADATADATADATADATA',
    'DATADATADATADATA...DATADATADATADATADATADATA',
    'DATADATADATADATA...DATADATADATADATADATADATA',
    'DATADATADATADATA...DATADATADATADATADATADATA',
    'DATADATADATADATA...DATADATADATADATADATADATA',
    'DATADATADATADATA...DATADATADATADATADATADATA',
);
$record_hashes = array_map(function($record_string) {
    return array(
        'COUNTY-DISTRICT' => substr($record_string, 0, 4),
        'BLOCK' => substr($record_string, 4, 9),
        'LOT' => substr($record_string, 13, 9),
        ...
    );
}, $record_strings);

You now have an array of hashes that you can more easily do things with, like iterate over and insert into a database.

000
  • 26,951
  • 10
  • 71
  • 101