In a web app my users upload a csv file and I need to check it on the way into the database. Part of the information forms a simple hierarchical dataset, so I want to make sure that rows are unique. Here's what I was thinking of doing, with an example.
CSV includes these rows...
field5 field6 field7
fred bert apple
fred bert apple
fred george
fred george pear
will tom orange
will tom plum
I was going to absorb them into a PHP array like this.
$unique_fields = array();
while( $row = fgetcsv($handle) ){
if( $row[5] ){
if( !array_key_exists( $row[5] , $unique_fields ) ){
$unique_fields[ $row[5] ] = array();
}
if( $row[6] ){
if( !array_key_exists( $row[6] , $unique_fields[ $row[5] ] )){
$unique_fields[ $row[5] ][ $row[6] ] = array();
}
if( $row[7] ){
if( !array_key_exists( $row[7], $unique_fields[ $row[5] ][ $row[6] ] )){
$unique_fields[ $row[5] ][ $row[6] ][ $row[7] ] = array();
}
}
}
}
}
If this is a good idea (and I'm open to suggestions) - what's the best way after that? I want to transfer the array into my database so that it looks like the csv example above, but without duplicates.