-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?

1
  • 1
    fputcsv - 'fields - An array of strings.' - Example Commented Sep 18, 2019 at 9:48

1 Answer 1

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]);
Sign up to request clarification or add additional context in comments.

1 Comment

It worked, thank you. Thought it was way more complicated than that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.