From 54e846ee30335056942b1d0eddfe9bc232216615 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Bl=C3=A4ul?= Date: Sun, 21 Jan 2018 16:35:29 +0100 Subject: [PATCH] Construct array key just once instead for better readability --- parsecsv.lib.php | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/parsecsv.lib.php b/parsecsv.lib.php index be40a26..60d3efe 100644 --- a/parsecsv.lib.php +++ b/parsecsv.lib.php @@ -609,8 +609,9 @@ class parseCSV { $this->error = 2; $error_row = count($rows) + 1; $error_col = $col + 1; - if (!isset($this->error_info[$error_row . '-' . $error_col])) { - $this->error_info[$error_row . '-' . $error_col] = array( + $index = $error_row . '-' . $error_col; + if (!isset($this->error_info[$index])) { + $this->error_info[$index] = array( 'type' => 2, 'info' => 'Syntax error found on row ' . $error_row . '. Non-enclosed fields can not contain double-quotes.', 'row' => $error_row, @@ -636,8 +637,9 @@ class parseCSV { $error_row = count($rows) + 1; $error_col = $col + 1; - if (!isset($this->error_info[$error_row . '-' . $error_col])) { - $this->error_info[$error_row . '-' . $error_col] = array( + $index = $error_row . '-' . $error_col; + if (!isset($this->error_info[$index])) { + $this->error_info[$index] = array( 'type' => 1, 'info' => 'Syntax error found on row ' . (count($rows) + 1) . '. ' . @@ -671,13 +673,17 @@ class parseCSV { $head = $row; } elseif (empty($this->fields) || (!empty($this->fields) && (($this->heading && $row_count > 0) || !$this->heading))) { if (!empty($this->sort_by) && !empty($row[$this->sort_by])) { - if (isset($rows[$row[$this->sort_by]])) { - $rows[$row[$this->sort_by] . '_0'] = &$rows[$row[$this->sort_by]]; - unset($rows[$row[$this->sort_by]]); - for ($sn = 1;isset($rows[$row[$this->sort_by] . '_' . $sn]); $sn++) {} - $rows[$row[$this->sort_by] . '_' . $sn] = $row; + $sort_field = $row[$this->sort_by]; + if (isset($rows[$sort_field])) { + $rows[$sort_field . '_0'] = &$rows[$sort_field]; + unset($rows[$sort_field]); + $sn = 1; + while (isset($rows[$sort_field . '_' . $sn])) { + $sn++; + } + $rows[$sort_field . '_' . $sn] = $row; } else { - $rows[$row[$this->sort_by]] = $row; + $rows[$sort_field] = $row; } } else {