在php中将csv文件导入mysql - 如何忽略空行?

时间:2016-12-29 04:42:44

标签: php mysql csv

如果我尝试导入csv文件,则还会导入所有空行。

我有一个带有csv文件的表单

以下是表单操作代码

$file = $_FILES['file']['tmp_name'];
$allowedExts = array("csv");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);

if (!in_array($extension, $allowedExts)) {
        $this->session->set_flashdata('msg', 'Please upload a csv file format.');
    }
    else {
    $handle = fopen($file, "r");
    $c = 0;
    $flag = true;
    while(($filesop = fgetcsv($handle, 0, ",")) !== false)
    {
        if($flag) { $flag = false; continue; } //to skip the title row

        if($filesop != NULL ) {  //CONDITION TO CHECK EMPTY ROWS (DOES not work..)

       $data[] = array(
                'id'=>0, //uploader user id
                'field1' => (isset($filesop[14]) ? $filesop[14] : "" ),
                'field2' => (isset($filesop[15]) ? $filesop[15] : "" ),
                'field3' => (isset($filesop[1]) ? $filesop[1] : "" ),
                'field4' => (isset($filesop[13]) ? $filesop[13] : "" ),
                'field5' => (isset($filesop[6]) ? $filesop[6] : "" ),
                'field6' => (isset($filesop[2]) ? $filesop[2] : "" ),
                'field7' => (isset($filesop[5]) ? $filesop[5] : "" ),
                'field8' => (isset($filesop[3]) ? $filesop[3] : "" ),
                'field9' => (isset($filesop[4]) ? $filesop[4] : "" ),
            );
        }
    }
           echo "<pre>";
    print_r($data);
    exit;

请帮我修复,我该怎么办才能将空行导入

2 个答案:

答案 0 :(得分:1)

我会使用array_filter从数组中删除空元素,然后检查是否有任何内容。

while(($filesop = fgetcsv($handle, 0, ",")) !== false){

  $empty_filesop = array_filter( array_map('trim', $filesop));

  if( !empty( $empty_filesop ) ){

      .....
  }
}

带有trim的数组映射,将修剪数据以删除具有空格的行,例如,{space},将其视为:

  foreach( $empty_filesop as &$item ) 
     $item = trim( $item );

另外,请注意PHP会将一些内容视为空,您可能不会将其视为空,并将使用标准array_filter删除它们,例如0中的,0,array( 0, 'blah' )元素0将被删除。如果您需要更高的准确度,可以执行以下操作:

 $empty_filesop = array_filter(
        array_map('trim', $filesop),
        function( $item ){
            return strlen( $item ); //return the string length of $item ( 0 is false > 0 is true )
        }
 );

如果您依赖于元素位置,如果有空行则会更改,因此请复制它以进行检查。例如,使用$empty_filesop,这样就不会使用过滤器改变实际行...

http://php.net/manual/en/function.array-filter.php

http://php.net/manual/en/function.array-map.php

http://php.net/manual/en/function.trim.php

对于标题,请执行此操作(我不知道orignal键):

$headers = false;
$order = ['id'=>0, 'org3'=>'', 'org2'=>'', 'org1'=>''];
while(($filesop = fgetcsv($handle, 0, ",")) !== false)
{
    if(!$headers) { $headers = $filesop; continue; } 

    $empty_filesop = array_filter( array_map('trim', $filesop));

    if( !empty($empty_filesop)){
      //check for empty rows
      if( count( $headers ) == count($filesop) ) {  
       //check for rows with extra or missing delimiters.
        $data[] = array_replace( $order, array_combine( $headers, $filesop)); //combine with original headers, and replace the data in the 'order row' to re-order.
       }else{
           //row is missing or has extra delimiters.
       }
    }
}

答案 1 :(得分:0)

更改

host :'127.4.188.2',
port :'3306', // if this doesn't work try removing port once
user:'adminfxxxxx',
password:'xxxxxxxxx',
database:'xxxxxxxxx', //specify database Name

if ($filesop != null) 

查看文档的返回值部分:http://php.net/manual/en/function.fgetcsv.php