title: Why Export to CSV Data only showing in one column
body: <p>We have many dynamic tables being generated in the process of our application. so we are trying to export the data from those dynamically generated table column data to .csv file.</p>
  <pre><code>$secondsql = &quot;SELECT * FROM &quot;.$reslt-&gt;dname.&quot; WHERE userid=:userid AND eventid=:eventid&quot;;
      
  $querysec = $dbh -&gt; prepare($secondsql); 
  $querysec-&gt; bindParam(':userid', $usrid, PDO::PARAM_STR);  
  $querysec-&gt; bindParam(':eventid', $exporteventid, PDO::PARAM_STR);                                     
  $querysec-&gt; execute();
  $ressec = $querysec -&gt; fetchAll(PDO::FETCH_OBJ);
  </code></pre>
  <p>This is how we are fetching the data and converting it to json and getting key value pairs and passing onto fputcsv
  <code>$jsonString = json_encode($ressec);</code></p>
  <pre><code>        $jsonDecoded = json_decode($jsonString, true);
          $keys = array_keys(json_decode($jsonString, true));
          $csvHeader=array();
          $csvData=array();
          
          foreach($jsonDecoded as $key =&gt; $val) {
             
              if (!$val) {
              }else{
                  if(!$csvHeader){
                      $csvHeader = array_keys($val);
                  }
             }
          }
          
          $kcnt = count($csvHeader);
          
          $tempstring = &quot;&quot;;
          
          foreach($jsonDecoded as $val2) {
              $arrvals = array_values($val2);
              $resultStringValues = implode(&quot;,&quot;, $arrvals);
              
              $temparray=array();
              array_push($temparray,$resultStringValues);
            
              array_push($csvData,$temparray);
          }
          $tempstring = rtrim($tempstring, &quot;,&quot;);
          $delimiter = &quot;,&quot;; 
          $filename = $reslt-&gt;eventname. date('Y-m-d') . &quot;.csv&quot;; 
          $f = fopen('php://memory', 'w'); 
      
          fputcsv($f, $csvHeader, $delimiter);
         
          foreach($csvData as $fields){
              fputcsv($f,$fields,$delimiter);
          }
         
          fseek($f, 0); 
          header('Content-Type: text/csv'); 
          header('Content-Disposition: attachment; filename=&quot;' . $filename . '&quot;;');
          fpassthru($f); 
  </code></pre>
  <p>The result for this is
  <a href="https://i.stack.imgur.com/s2cAZ.png" rel="nofollow noreferrer">enter image description here</a></p>
  <p>why it is not splitting the data in the appropriate columns?
  my $csvHeader and $csvData array outputs</p>
  <pre><code>Array
  (
      [0] =&gt; id
      [1] =&gt; eventid
      [2] =&gt; userid
      [3] =&gt; fullname
      [4] =&gt; email
      [5] =&gt; mobile
      [6] =&gt; reg_date
  )
  1
  Array
  (
      [0] =&gt; Array
          (
              [0] =&gt; 21,22,19,khasim,khasim@khasim.com,8786468768,2022-05-30 00:00:00
          )
  
      [1] =&gt; Array
          (
              [0] =&gt; 22,22,19,tanveer,khasim@ism.com,2343434,2022-05-30 01:00:00
          )
  
      [2] =&gt; Array
          (
              [0] =&gt; 23,22,19,tan,tan@gmail.com,24343,2022-05-30 00:00:00
          )
  
      [3] =&gt; Array
          (
              [0] =&gt; 24,22,19,raju,raj@kaju.com,234324324,2022-05-30 01:00:00
          )
  
      [4] =&gt; Array
          (
              [0] =&gt; 26,22,19,tan,tan@gmail.com,24343,2022-05-30 00:00:00
          )
  
      [5] =&gt; Array
          (
              [0] =&gt; 27,22,19,raju,raj@kaju.com,234324324,2022-05-30 01:00:00
          )
  
      [6] =&gt; Array
          (
              [0] =&gt; 29,22,19,raju,raj@kaju.com,234324324,2022-05-30 01:00:00
          )
  
      [7] =&gt; Array
          (
              [0] =&gt; 30,22,19,raju,raj@kaju.com,234324324,2022-05-30 01:00:00
          )
  
      [8] =&gt; Array
          (
              [0] =&gt; 31,22,19,tan,tan@gmail.com,24343,2022-05-30 00:00:00
          )
  
      [9] =&gt; Array
          (
              [0] =&gt; 32,22,19,raju,raj@kaju.com,234324324,2022-05-30 01:00:00
          )
  
      [10] =&gt; Array
          (
              [0] =&gt; 33,22,19,raju,raj@kaju.com,234324324,2022-05-30 01:00:00
          )
  
      [11] =&gt; Array
          (
              [0] =&gt; 34,22,19,ttttt,tttt@ttt.com,234324324,2022-05-30 01:00:00
          )
  
      [12] =&gt; Array
          (
              [0] =&gt; 35,22,19,mohan,mohan@mohan.com,123456789,2022-05-30 01:00:00
          )
  
      [13] =&gt; Array
          (
              [0] =&gt; 36,22,19,seet,seeet@mohan.com,123456789,2022-05-30 01:00:00
          )
  
      [14] =&gt; Array
          (
              [0] =&gt; 37,22,19,bhadrachalam,bhadrachalam@gmail.com,865956854,2022-06-15 07:17:49
          )
  
  )
  </code></pre>
  <p>I messed up the array and done all unnecessary conversions, as no one pointed at what I did wrong I managed to solve so pasting the solution for others</p>
  <pre><code>$secondsql = &quot;SELECT * FROM &quot;.$reslt-&gt;dname.&quot; WHERE userid=:userid AND eventid=:eventid&quot;;
      
      $querysec = $dbh -&gt; prepare($secondsql); 
      $querysec-&gt; bindParam(':userid', $usrid, PDO::PARAM_STR);  
      $querysec-&gt; bindParam(':eventid', $exporteventid, PDO::PARAM_STR);                                     
      $querysec-&gt; execute();
      //$ressec = $querysec -&gt; fetchAll(PDO::FETCH_OBJ);
      $table_fields = array_keys($querysec-&gt;fetch(PDO::FETCH_ASSOC));
      $ressec = $querysec -&gt; fetchAll(PDO::FETCH_ASSOC);
      
      
          $delimiter = &quot;,&quot;; 
              $filename = $reslt-&gt;eventname. date('Y-m-d') . &quot;.csv&quot;; 
              $f = fopen('php://memory', 'w'); 
      
          fputcsv($f, $table_fields, $delimiter);
         
          foreach($ressec as $fields){
                          
              fputcsv($f,$fields,$delimiter);
               
          }
          fseek($f, 0); 
          header('Content-Type: text/csv'); 
          header('Content-Disposition: attachment; filename=&quot;' . $filename . '&quot;;'); 
          fpassthru($f);
  </code></pre>