How could I merge the loops below?

2

This is the spreadsheet:

Thisisthecode:

publicfunctionloadExcel($file,$dir){$objReader=PHPExcel_IOFactory::createReader('Excel2007');$filterValidation=newApplication_Model_Filter_FilterValidation();if(file_exists($dir.DS.$file)){$objPHPExcel=$objReader->load($dir.DS.$file);$arrayData=array();//laço1foreach($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ço2foreach($dataas$key=>$value){if(count($value)){//laço3foreach($valueas$letter=>$val){if($header[$letter]!=''&&!is_null($header[$letter])&&!is_numeric($header[$letter])){if(!is_null($val)){$result[$header[$letter]][]=$val;}}}}}$new=array();//laço4foreach($resultas$key=>$val){if(count($val)){//laço5foreach($valas$idx=>$dado){if(count($dado)){$new[$idx][$key]=$dado;}}}}return$new;}}}

Result:

Array([0]=>Array([NOME]=>LuizFelipeMachado[USUARIO]=>alunoguten001[SENHA]=>aluno001)[1]=>Array([NOME]=>MariaRitadeCássia[USUARIO]=>alunoguten002[SENHA]=>aluno002))

PS:partofthissolutionhasbeenresolved this question , but I do not think it is necessary to make three foreach to handle the data, I believe that it is possible to improve this in a single loop, how could it improve?

    
asked by anonymous 24.02.2017 / 20:52

1 answer

0

I'll give you a suggestion, save this spreadsheet in CSV and use the following code.

function loadFile ( $file ) 
{
    $data = [];
    if ( ( $handle = fopen( $file, "r" ) ) !== FALSE) {
        $i = 0;
        while ( ( $data = fgetcsv( $handle, 1000, ";" ) ) !== FALSE ) {

            if ( $i == 0 ) {
                $i++;
                continue;
            }

            $data[] = [
                'nome'    => utf8_encode( $data[0] ),
                'usuario' => utf8_encode( $data[1] ),
                'senha'   => utf8_encode( $data[2] )
            ];

            $i++;
        }
    }

    return $data;
}

Q: This will only be useful for you with CSV files only.

    
02.06.2017 / 18:54