LucianoCharles2017

fputcsv

Oct 7th, 2018
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.24 KB | None | 0 0
  1. <?php
  2.  
  3. $dados = array
  4.     (
  5.     '0' => array
  6.         (
  7.         'idPaciente' => 150393,
  8.         'paciente' => array
  9.             (
  10.             'idPaciente' => 150393,
  11.             'nomePaciente' => 'ANA CAROLINA TURRIONI AZEVEDO PACHECO'
  12.         )
  13.     ),
  14.     '1' => array
  15.         (
  16.         'idPaciente' => -1219,
  17.         'paciente' => array
  18.             (
  19.             'idPaciente' => 150393,
  20.             'nomePaciente' => 'FULANO DE TAL'
  21.         )
  22.     )
  23. );
  24.  
  25. /*
  26.  * Aqui crio uma variavel que vai receber os campos que desejo
  27.  */
  28. $armazenar_estes = null;
  29.  
  30. foreach ($dados as $lista) {
  31.  
  32.     $armazenar_estes[] = [
  33.         /*
  34.          * aqui eu crio indices e passo valores para podes extrair no freach
  35.          * do fputcsv
  36.          */
  37.         'idPaciente' => $lista['idPaciente'],
  38.         'nomePaciente' => $lista['paciente']['nomePaciente']
  39.     ];
  40. }
  41.  
  42. $filename = 'Discador.csv';
  43.  
  44. header("Content-type: text/csv");
  45.  
  46. header("Content-Disposition: attachment; filename=$filename");
  47.  
  48. $delimiter = ";";
  49.  
  50. $output = fopen("php://output", "w");
  51.  
  52. $header = array_keys($armazenar_estes[0]);
  53.  
  54. fputcsv($output, $header, $delimiter);
  55.  
  56. foreach ($armazenar_estes as $row) {
  57.     fputcsv($output, $row, $delimiter);
  58. }
  59.  
  60. fclose($output);
Advertisement
Add Comment
Please, Sign In to add comment