What I'm trying to do is read in a worksheet from an excel file and only keep certain columns based on a list in another array. I able to read in the excel sheet using PHPExcel, but haven't been successfully filtering out the unwanted column keys. I have been searching the web and been trying to apply the following: array_intersect_key, functions, array_filter and no success.
<?php /** Include path **/ set_include_path(get_include_path() . PATH_SEPARATOR . '../../../Classes/'); /** PHPExcel_IOFactory */ include 'PHPExcel/IOFactory.php'; //$inputFileType = 'Excel5'; // $inputFileType = 'Excel2007'; // $inputFileType = 'Excel2003XML'; // $inputFileType = 'OOCalc'; // $inputFileType = 'Gnumeric'; $inputFileName = './sampleData/example1.xls'; $sheetname = 'Visual'; $inputFileType = PHPExcel_IOFactory::identify($inputFileName); echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory with a defined reader type of ',$inputFileType,'<br />'; $objReader = PHPExcel_IOFactory::createReader($inputFileType); echo 'Loading Sheet "',$sheetname,'" only<br />'; $objReader->setLoadSheetsOnly($sheetname); $objPHPExcel = $objReader->load($inputFileName); echo '<hr />'; echo $objPHPExcel->getSheetCount(),' worksheet',(($objPHPExcel->getSheetCount() == 1) ? '' : 's'),' loaded<br /><br />'; $loadedSheetNames = $objPHPExcel->getSheetNames(); foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) { echo $sheetIndex,' -> ',$loadedSheetName,'<br />'; } $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true); // Removes NULL values foreach($sheetData as $key => &$row) { $row = array_filter($row,function($cell){return !is_null($cell);}); if (count($row) == 0) { unset($sheetData[$key]); } } unset ($row); unset ($sheetData[1]); // Remove row 1 of spreadsheet unset ($sheetData[2]); // Remove row 2 of spreadsheet // columns what to keep, this is just example $col_keep = array('A', 'B', 'D', 'K'); foreach($sheetData as $k=>$v) { foreach($v as $c=>$vb){ echo "$c<br>"; // this display the columns keys e.g A,B,C,etc... // How to filter here with array $col_keep? } } ?>