从csv到数据库获取层次结构的最简洁方法是什么?

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.

OK, no one wanted a stab at it, so I persevered and this is what I came up with. I'll watch the page in case someone has a better solution. Thanks for looking.

if( count($unique_fields) ){
    foreach( $unique_fields as $k1=>$v1 ){
        if( is_array($v1) ){
            foreach( $v1 as $k2=>$v2 ){
                if( is_array($v2) ){
                    foreach( $v2 as $k3=>$v3 ){
                        //insert $k1, $k2, $k3
                    }
                }else{
                    //insert $k1, $k2
                }
            }
        }else{
            //insert $k1 on its own
        }
    }
}