how to convert array to csv in symfony 3

In symfony, when use function like findAll() it return Object array for that repository, but in my case I need to get data with custom by using createQueryBuilder but it return "mixed"

when dump result

array:380 [▼
  0 => array:12 [▼
    "id" => 1
    "email" => "[email protected]"
    "firstname" => "test"
    "lastname" => "test"
    "gender" => null
    "lastLogin" => null
    "createdAt" => DateTime @01111111111 {#1174 …1}
    "updatedAt" => null
    "source" => null
    "isActive" => true
    "abc" => "text"
    "companyabc" => "company name"
  ]

I try to use StreamedRespone to generate CSV like this:

$response = new StreamedResponse();
        $response->setCallback(
            function () use ($results) {
                $handle = fopen('php://output', 'w+');
                foreach ($results as $row) {
                    $data = [
                      $row->getId(),
                      $row->getEmail(),
                    ];
                    fputcsv($handle, $data);
                }
                fclose($handle);
            }
        );

but it not work

How can I convert this array data to CSV?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *