Export values from a php array to a . csv by inserting each value into a cell

Asked

Viewed 25 times

0

I’m trying to create a function that stores the values of an array in a . csv file, but inside the . csv is stored only the level of the parent array, for example: ArrayArrayArrayArrayArrayArray. The code of the function:

function convert_data_to_csv( $data ) {
    $content = array();
    foreach ( $data as $array ) {
        foreach ( $array as $row ) {
            $content .= $row;
            file_put_contents( 'http://127.0.0.1/proj/src/csv/certificados.csv', $content );
        }
    }
}

The array data passed as a function parameter are:

array(2) {
  [0]=>
  array(3) {
    [0]=>
    array(2) {
      ["fruta"]=>
      string(6) "banana"
      ["cor"]=>
      string(4) "azul"
    }
    [1]=>
    array(2) {
      ["fruta"]=>
      string(4) "maçã"
      ["cor"]=>
      string(5) "verde"
    }
  }

How can I modify the function code to return . csv with each array value in a cell? Like in this example:

banana,azul
maçã,verde

1 answer

0

See if this will work, I believe it’s a matter of putting one more foreach in his array has 3 levels.

function convert_data_to_csv( $data ) {
    $content = array();
    foreach ( $data as $array ) {
        foreach ( $array as $row ) {
            foreach ( $row as $novarow ) {
                $content .= $novarow.';';
                file_put_contents( 'http://127.0.0.1/proj/src/csv/certificados.csv', $content );
            }
        }
    }
}

EDIT 1

In my codes I usually use this function to generate my csvs and related from arrays.

function array_para_csv(array &$array){
       
        // Formatação Coluna;
        $header = '';
        $content = '';

        $flag = false;
    
        foreach($array as $row) {
        
            if(!$flag) {
              // display field/column names as first row
              $header .= implode("\t", array_keys($row)) . "\r\n";
              $flag = true;
            }
            $content .= implode("\t", array_values($row)) . "\r\n";
        }

        return $header.$content;


    }

function cabecalho_download_csv($filename) {
        
        // desabilitar cache
        $now = gmdate("D, d M Y H:i:s");
        header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
        header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
        header("Last-Modified: {$now} GMT");

        // forçar download
        // header ("Content-type: application/x-msexcel");
        // header('Content-Type: application/vnd.openxmlformats- officedocument.spreadsheetml.sheet');  
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");

        // disposição do texto / codificação
        header("Content-Disposition: attachment;filename={$filename}");
        header("Content-Transfer-Encoding: binary");

    }

cabecalho_download_csv("lista_cadastro_".date("YmdHis") . ".csv");
        echo array_para_csv($array);
        exit();

However the array has to be passed in a specific structure as below, where the key becomes the column name:

$frutas[1] = 'banana';
$frutas[2] = 'maçã';
  • I put one more foreach in the array, but it returns the csv with the data in the same cell: bananaazulmaçãverde

  • If you concatenate a ; the return does not solve, see how I put in the edited code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.