Creating CSV file using data from database, gives "mosaic, straggly" results
<p>First part of code works fine, but the second one (displaying results), works wrongly. If i am inserting <code>$slugs = explode(',', $slugs)</code> inside <code>foreach ($pma...)</code>, it gives me <code>explode() expects parameter 2 to be string, array given</code> error. If i keep it as it now, it works, but the results is given in "mosaic, straggly" mode, with multiple same results. Even <code>print_r()</code> shows 800 results per one row (it is nonsense).</p>

<p>How to fix this?</p>

<pre><code>$data = fopen('php://output', 'w');

$fields = rtrim($_GET['fieldnames'], ",");
$slugs = rtrim($_GET['slugs'], ",");

$fieldnames = array();
$fieldslugs = array();

$pma = DB::table... // long query

$fields = explode(',', $fields);
   foreach ($fields as $field) {
      $fieldnames[] = $field;
}

fputcsv($data, $fieldnames);


$slugs = explode(',', $slugs);

foreach ($pma as $p) {

  foreach ($slugs as $slug) {
    $fieldslugs[] = $p-&gt;$slug;
  }

  fputcsv($data, $fieldslugs);

}
</code></pre>

<p>Sorry for bad english, and tanks for any answers!</p>