From ab9e8a0af9b94c0817598f9b7798ccea93a81de3 Mon Sep 17 00:00:00 2001 From: Susann Sgorzaly Date: Tue, 13 Nov 2018 18:53:08 +0100 Subject: [PATCH] fixes unparse bug if array ids doesn't begin on zero (comments#149) --- src/Csv.php | 2 +- tests/methods/UnparseTest.php | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Csv.php b/src/Csv.php index c3b6cc3..c901e71 100644 --- a/src/Csv.php +++ b/src/Csv.php @@ -846,7 +846,7 @@ class Csv { } // this is needed because sometime titles property is overwritten instead of using fields parameter! - $titlesOnParse = !empty($this->data) ? array_keys($this->data[0]) : array(); + $titlesOnParse = !empty($this->data) ? array_keys(reset($this->data)) : array(); // both are identical, also in ordering if (array_values($fields) === array_values($titlesOnParse)) { diff --git a/tests/methods/UnparseTest.php b/tests/methods/UnparseTest.php index 48ebb09..6083724 100644 --- a/tests/methods/UnparseTest.php +++ b/tests/methods/UnparseTest.php @@ -75,6 +75,12 @@ class UnparseTest extends Testcase { $this->unparseAndCompare($expected, $fields); } + public function testUnparseDefaultFirstRowMissing(){ + unset($this->csv->data[0]); + $expected = "column1,column2\rvalue3,value4\r"; + $this->unparseAndCompare($expected); + } + private function unparseAndCompare($expected, $fields = array()) { $str = $this->csv->unparse($this->csv->data, $fields); $this->assertEquals($expected, $str);