diff --git a/src/Csv.php b/src/Csv.php index c901e71..ec2e998 100644 --- a/src/Csv.php +++ b/src/Csv.php @@ -848,8 +848,8 @@ class Csv { // this is needed because sometime titles property is overwritten instead of using fields parameter! $titlesOnParse = !empty($this->data) ? array_keys(reset($this->data)) : array(); - // both are identical, also in ordering - if (array_values($fields) === array_values($titlesOnParse)) { + // both are identical, also in ordering OR we have no data (only titles) + if (empty($titlesOnParse) || array_values($fields) === array_values($titlesOnParse)) { return array_combine($fields, $fields); } diff --git a/tests/methods/UnparseTest.php b/tests/methods/UnparseTest.php index 6083724..f57a17a 100644 --- a/tests/methods/UnparseTest.php +++ b/tests/methods/UnparseTest.php @@ -81,6 +81,13 @@ class UnparseTest extends Testcase { $this->unparseAndCompare($expected); } + public function testUnparseDefaultWithoutData(){ + unset($this->csv->data[0]); + unset($this->csv->data[1]); + $expected = "column1,column2\r"; + $this->unparseAndCompare($expected); + } + private function unparseAndCompare($expected, $fields = array()) { $str = $this->csv->unparse($this->csv->data, $fields); $this->assertEquals($expected, $str);