-2

I'm currently working on a project where I have to access the objects from Audience Segments and export the data that comes together with it in an excel format.

I'm still new to PHP and I can't seem to figure my way around this. Here's a sample code

if ($page->getResults() !== null) {
    $totalResultSetSize = $page->getTotalResultSetSize();
    $i = $page->getStartIndex();


    $myfile = fopen($fileName.".xls", "a") or die("Unable to open file!");

    foreach ($page->getResults() as $audienceSegment) {

        $id =  $audienceSegment->getId(); //long
        $name =  $audienceSegment->getName(); //string
        $size = $audienceSegment->getSize(); //long

        fputcsv($myfile,"$id, $name, $size");
        $i++;

    }
    fclose($myfile);
}

As you can see in the code, I'm trying to export the data from the variables $id, $name, and $size. Since I'm using fputcsv, I'm getting this error,

**Warning: fputcsv() expects parameter 2 to be array**

Hence, that's the reason why I want the objects to come in an array. Can someone help me?

oracle02
  • 55
  • 1
  • 10
  • 1
    [`fputcsv`](https://www.php.net/manual/en/function.fputcsv.php) - '*fields - An array of strings.*' - [Example](https://www.php.net/manual/en/function.fputcsv.php#refsect1-function.fputcsv-examples) – Script47 Sep 18 '19 at 09:48

1 Answers1

1

Have you tried to use an array instead? You are passing a second argument as a string. Instead of:

fputcsv($myfile,"$id, $name, $size");

try:

fputcsv($myfile,[$id, $name, $size]);
Serving Quarantine period
  • 66,345
  • 10
  • 43
  • 85
Lukasz Formela
  • 424
  • 4
  • 14