简体   繁体   中英

Should I use a complex multi-dimensional array or create an object?

I'm writing a script to import csv's into MySQL. The csv's will have variable numbers of fields, different data types and field sizes.

My current plan is to do a pass through the csv and collect statistical information on it to inform the MySQL CREATE TABLE query.

My conception was to create an array in this format:

$csv_table_data = array(
  ['columns'] => array(
    [0] => array(
      ['min_size'] = int,
      ['max_size'] = int,
      ['average'] = int
      ['type'] = string
    ),
    [1] => array(
      ['min_size'] = int,
      ['max_size'] = int,
      ['average'] = int
      ['type'] = string
    ),
    [2] => array(
      ['min_size'] = int,
      ['max_size'] = int,
      ['average'] = int
      ['type'] = string
    ),
  ),
  ['some_other_data'] = array()
);

So, by accessing $csv_table_data['columns'][$i] I can access each column's attributes and use that to create the MySQL table. Besides $csv_table_data['columns'] I also have other data like total_rows, number_of_fields, and may add more.

I can gather all this data in one pass, create the appropriate table, and then populate it in a second pass.

I haven't used much object-oriented programming, but with all this data should I consider creating an object with these various properties, rather than creating this complex array?

What do you think about it in terms of readability, maintainability, speed of execution, and any other considerations that occur to you?

Thanks

I think you should use Classes, not only one big Object. Maybe you split it up to 2 or 3 Classes. It's much more cleaner as only arrays.

Something like this

class Table{

private $data = array();
private $otherData = 'what_ever';

public function getData(){
 return $this->data;
}

public function addData(Row $row){
 $this->data[] = $row;
}

//Add getter and setter


}

class Row{

private $min_size;
private $max_size;
private $avg_size;
private $type;

public function setMinSize($minSize){
$this->min_size = $minSize;
}

public function getMinSize(){
 return $this->min_size;
}


//Add more getter and setter

}

if you have a limited number of files you want to insert you can use MySql's internal functions. To import the datafile, first upload it to your home directory, so that the file is now located at /importfile.csv on our local system. Then you type the following SQL at the mysql prompt: LOAD DATA LOCAL INFILE '/importfile.csv' INTO TABLE test_table FIELDS TERMINATED BY ',' LINES TERMINATED BY '\\n' (field1, filed2, field3);

The above SQL statement tells the MySQL server to find your INFILE on the LOCAL filesystem, to read each line of the file as a separate row, to treat any comma character as a column delimiter, and to put it into your MySQL test_table as columns field1, field2, and field3 respectively. Many of the above SQL clauses are optional and you should read the MySQL documentation on the proper use of this statement.

On the other hand if you have to many files or you want to allow users to upload csv which you then in turn add them to the DB, I recommend using the 'array' version as it seems simpler to traverse.

Cheers, Denis

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM