title: PDO SQL QUERY - Fill an array with results PHP
body: <p>after my sql query, using PDO and fetch() function, i'd like to fill up a 2D array, that i can then display or export in my website.</p>
  
  <p>I am having a weird issue in the process: the first two rows fill up fine in their own sub-array, but the third one doesn't and continues filling up the second sub-array.</p>
  
  <p>I'd like to achieve this:</p>
  
  <pre><code>$tableau_pour_lexport= array (
   0 =&gt; "filename",
   1 =&gt; array (colomn name1 .. colomn name n )
   2=&gt; array (value 1... value n)
   3 =&gt; array (value 1 .. value n)
   .... );
  </code></pre>
  
  <p>But i get something like this :</p>
  
  <pre><code>$tableau_pour_lexport= array (
       0 =&gt; "filename",
       1 =&gt; array (colomn name1 .. colomn name n )
       2=&gt; array (value 1... value n)
       3 =&gt; array (value 1 .. value n, value 1....value n, value 1... value n)
       );
  </code></pre>
  
  <p>Here's my code :</p>
  
  <pre><code>&lt;?php
  
  $sql_query="SELECT id_dossier, type_sortie, date_incident, no_train, commentaire, conclusion_cause, retard_min, etat, no_rame1 FROM sortie_prevue
  UNION 
  SELECT id_dossier, type_sortie, date_incident, no_train, commentaire, etat, conclusion_cause, retard_min, no_rame1 FROM sortie_non_prevue
  ORDER BY date_incident LIMIT 3";
  
  $reponse = $bdd-&gt;query("$sql_query");
  </code></pre>
  
  <p><strong>#file name line</strong></p>
  
  <pre><code>$tableau_pour_lexport[0]="titre du fichier";
  </code></pre>
  
  <p><strong>#column name line</strong></p>
  
  <pre><code>$premiere_ligne= $reponse-&gt;fetch(PDO::FETCH_ASSOC);
  foreach ($premiere_ligne as $cle=&gt;$element1 )
  {
  $colonnes[]=$cle;
  }
  $tableau_pour_lexport[1]=$colonnes;
  </code></pre>
  
  <p><strong>#value while supposed to fill up the rest</strong></p>
  
  <pre><code>while ($donnees = $reponse-&gt;fetch(PDO::FETCH_NUM))
      { 
          foreach ($donnees as $valeur )
          {
              $ligne[]=$valeur;
          }
  
          $tableau_pour_lexport[]=$ligne;
      }   
  
      print_r($tableau_pour_lexport);
  
      $reponse-&gt;closeCursor(); // Termine le traitement de la requête
      ?&gt;
  </code></pre>
  
  <p>Any help would be welcome !
  Any advice if there is a smarter way to deal my sql data ?</p>