Failed array fputcsv in PHP

0

I'm having problems creating csv files with fputcsv. The fact is that I go through the database while I show the columns and keep them in the csv, but in the loop, I also make a call to a function which returns a string. I make an array_push to add it to the array that contains the rows and I get an error: fputcsv () expects parameter 2 to be array, integer given. I leave the code for you to understand me better:

$resultados = Yii::app()->db->createCommand($query)->queryAll();

//Consulta SQL
                json_encode(array('response' => 'true'));
                mb_internal_encoding("UTF-8");
                $codificacion = mb_internal_encoding();

                try {
                    //Fichero
                    $archivo=fopen(getcwd()."/assets/log/".$nombre_fichero."_".$date.".csv","a+");
                    //Cabecera
                    fputcsv($archivo, $cabecera, $delimeter);
                    foreach ($resultados as $line) {  
                        $direcciones = $this->direcciones($token, $line, $archivo, $codificacion);
                        $lines = array_push($line, $direcciones);
                        fputcsv($archivo, $lines, $delimeter);//Aquí me da el error en el parámetro 2       
                    }
                } catch (Exception $e) {
                    fputs($archivo,"Error al crear fichero CSV-> Error: ".$e->getMessage().PHP_EOL);
                }
                fclose($archivo);
    
asked by Csc99 18.07.2018 в 14:25
source

1 answer

0

Use array_merge :

foreach ($resultados as $line) {  
    $direcciones = $this->direcciones($token, $line, $archivo, $codificacion);
    $lines = array_merge($line, $direcciones);
    fputcsv($archivo, $lines, $delimeter);
}

Greetings!

    
answered by 18.07.2018 / 15:20
source