0

I have this Data Array like this

I have stored this value in $data variable

enter image description here

I want to export this data into Excel or CSV

I have tried multiple ways & checked a lot articles but still can't find anything

1
  • 2
    "I have tried multiple ways & checked a lot articles but still can't find anything" for real? you didnt even stumble upon fputcsv? not even this qa? not even a stroll on packagist? Commented Apr 30, 2022 at 13:55

1 Answer 1

0

You can use plain PHP only without any lib to create csv file. I took a test array for the example below.

The important step is only to convert your array in a comma separated string.

example

<?php

$arr = [
    [
        "id" => 1, "name" => "abc"
    ],
    [
        "id" => 2, "name" => "def"
    ],  
];

$data = [];
foreach($arr as $val) {
    $str = implode(',', $val );
    $data[] = $str;
}

$handle = fopen('export.csv', 'w');

foreach ($data as $row) {
    fputcsv($handle, $row);
}

fclose($handle);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.