2
This is the spreadsheet:
This is the code:
public function loadExcel($file, $dir)
{
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$filterValidation = new Application_Model_Filter_FilterValidation();
if (file_exists($dir . DS . $file)) {
$objPHPExcel = $objReader->load($dir . DS . $file);
$arrayData = array();
//laço 1
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$arrayData[$worksheet->getTitle()] = $worksheet->toArray(null, true, true, true);
}
$sheetData = array_values($arrayData)[0];
$header = $sheetData[1];
unset($sheetData[1]);
$data = array_values($sheetData);
//return $data;
if(count($data)) {
$result = array();
//laço 2
foreach ($data as $key => $value) {
if (count($value)) {
//laço 3
foreach ($value as $letter => $val) {
if ($header[$letter] != '' && !is_null($header[$letter]) && !is_numeric($header[$letter])) {
if (!is_null($val)) {
$result[$header[$letter]][] = $val;
}
}
}
}
}
$new = array();
//laço 4
foreach ($result as $key => $val) {
if (count($val)) {
//laço 5
foreach ($val as $idx => $dado) {
if (count($dado)) {
$new[$idx][$key] = $dado;
}
}
}
}
return $new;
}
}
}
Upshot:
Array
(
[0] => Array
(
[NOME] => Luiz Felipe Machado
[USUARIO] => alunoguten001
[SENHA] => aluno001
)
[1] => Array
(
[NOME] => Maria Rita de Cássia
[USUARIO] => alunoguten002
[SENHA] => aluno002
)
)
PS: part of this solution has been resolved in this question, but I don’t think it’s necessary to do three foreach to treat the data, I believe that can improve this in a single loop, I just do not know how could improve?
You could trade these foreach loops for loops for, so the memory consumption would be better, unfortunately a lot can’t be done, since php has few features and is terrible on clean code and optimization.
– Jaedson Barbosa
I’m not familiar with the Phpexcel library. I could post the output of the var_dump($data) command before the if(Count($data) command)) ?
– jlHertel
what exactly the code does to generate this result?
– RFL