组合的两个数组和返回的结果具有空键和值

时间:2017-10-30 06:41:48

标签: arrays excel codeigniter

我有一张excel表,可以读取并转换为数组。 这是代码:

    $filename = $_FILES['importProjects']['tmp_name'];
    $objPHPExcel = PHPExcel_IOFactory::load($filename);

        $i = 0;
        $all_sheet = $objPHPExcel->getSheetCount(); //total number of sheets
        $arrayInsertProject =   array();
        $arrayUpdateProject =   array();
        while ($i < $all_sheet){
            $objPHPExcel->setActiveSheetIndex($i); 
            $allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
            $arrayCount = count($allDataInSheet);  // Here get total count of rows from the sheet
            $column = 'A';
            $project_cnt=1;
            $dataIns = array();
            for ($row = 1; $row <= $arrayCount; $row++) { //loop through rows
                if($row ==1){
                    $highestColumn = $objPHPExcel->getActiveSheet()->getHighestColumn();
                    $rowDataHeader = $objPHPExcel->getActiveSheet()->rangeToArray('A' . $row . ':' . $highestColumn . $row,
                    NULL,
                    TRUE,
                    FALSE);
                    $rowDataHeader  =   array_reduce($rowDataHeader, 'array_merge', array());       
                }else {
                    $highestColumn = $objPHPExcel->getActiveSheet()->getHighestColumn();

                    $rowData = $objPHPExcel->getActiveSheet()->rangeToArray('A' . $row . ':' . $highestColumn . $row,
                    NULL,
                    TRUE,
                    FALSE);
                   $dataIns =    array_combine($rowDataHeader, $rowData);
                }
            }
        }

当我打印这个数组$ dataIns时,我得到这样的结果:

Array
(
     [intProjectSlNo] => 1069
     [chvProjectNameEng] => New
     [slug] => this-is -test
     [nchvSecType] => 1754349
     [] => 
)

如何删除空键=&gt;价值对?

1 个答案:

答案 0 :(得分:0)

在代码的while循环后添加此内容

foreach($dataIns as $key=>$value)
{
    if(is_null($value) || $value == '')
        unset($dataIns[$key]);
}

print_r($dataIns);