0

I am having problem in exporting response in csv format in restler3 rc5.

public function downloadCSV()
{
$array = array(
    "foo", "bar"
);

return $array;
}

I am having the supported format line

 $r->setSupportedFormats('CsvFormat', 'JsonFormat'); 

but i am getting a empty csv.

Please help me on this. if there is any standard array format we need to return for converting it as csv. Please send me the format.

I have checked in the following page

vendor/Luracast/Format/CsvFormat.php

public function encode($data, $humanReadable = false)
{
    $char = Object::$separatorChar;
    Object::$separatorChar = false;
    $data = Object::toArray($data);
    Object::$separatorChar = $char;
    if (is_array($data) && array_values($data) == $data) {
        //if indexed array
        $lines = array();
        $row = array_shift($data);
        if (array_values($row) != $row) {
            $lines[] = static::putRow(array_keys($row));
        }
        $lines[] = static::putRow(array_values($row));
        foreach ($data as $row) {
            $lines[] = static::putRow(array_values($row));
        }
        return implode(PHP_EOL, $lines) . PHP_EOL;
    }
    throw new RestException(
        500,
        'Unsupported data for ' . strtoupper(static::EXTENSION) . ' format'
    );
}

The problem is the $lines which are getting returned are empty in the above scenario (ie

$array = array( "foo", "bar" );

The (array_keys($row) and array_values($row) lines in the encode function are empty because $row is not array its only text there.

I am stuck in it for many hours.

any help is highly appreciated.

Caleryn
  • 1,084
  • 3
  • 18
  • 23
user1602452
  • 65
  • 1
  • 1
  • 10

1 Answers1

0

Finally i have found the format of array to make it available in csv export

 $array [] = array("foo" =>"foo","bar"=>"bar","vvvv"=>"123");

i hope this will be helpful to others .

user1602452
  • 65
  • 1
  • 1
  • 10