0

I have used thephpleague library into laravel5.1 http://csv.thephpleague.com/inserting/

I want an example to add associative array into CSV. Right now I have used this functions insertOne($array) for header of CSV and insertAll($array) for their column values.

My code is -

    $path = public_path().'/csv/'.$filename;
    $csv = Writer::createFromPath(new \SplFileObject($path, 'w+'),   'a+');
    $csv->insertOne($headers);
    $csv->insertAll($data);
    $csv->output($path);

my $data array has an associative array (array inside the array) but insertAll() method gives me an error if $data has an associative array.

Please let me know how to deal with associative arrya?

John Slegers
  • 45,213
  • 22
  • 199
  • 169
Progi1990
  • 57
  • 1
  • 3
  • 13

1 Answers1

1

Transform the associative array to numeric indexed array like this

$csv->insertAll(array_map('array_values', $data));
beta-developper
  • 1,689
  • 1
  • 13
  • 24