summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUrban Müller2024-01-08 15:13:24 +0100
committerUrban Müller2024-01-08 15:13:45 +0100
commit6e0a397725d04e045e816c0f43fb6baef19341da (patch)
tree9c4a26864b481c6e99a42c380ef4af414311d1d1
parente1a4cea217551659720102f253d9e738b46ae537 (diff)
downloaditools-6e0a397725d04e045e816c0f43fb6baef19341da.tar.gz
itools-6e0a397725d04e045e816c0f43fb6baef19341da.tar.bz2
itools-6e0a397725d04e045e816c0f43fb6baef19341da.zip
add option for allowing missing column values at end of row
-rw-r--r--it_pipe.class6
1 files changed, 4 insertions, 2 deletions
diff --git a/it_pipe.class b/it_pipe.class
index fafc11f..03d9c69 100644
--- a/it_pipe.class
+++ b/it_pipe.class
@@ -184,6 +184,7 @@ function cols($collist, $separator = "\t")
* Return contents of pipe as associative records. Column titles read from first line, separators can be \t or ; or ,
* @param $p['forceschema'] ignore schema in file, replace it with this comma separated column list
* @param $p['fixcolnames'] replace non-identifier chars in colum names
+ * @param $p['allowmissingcols'] replace missing column values at end by null
* @param $p['it_error'] error params
* @return array of objects or empty array on format violations
*/
@@ -199,11 +200,12 @@ function csv($p = [])
$oldlocale = setlocale(LC_CTYPE, "0");
setlocale(LC_CTYPE, 'de_CH.iso-8859-1'); # this works for utf-8 as well
+ $nullcols = $p['allowmissingcols'] ? array_fill(0, count($schema), null) : [];
foreach ($this->lines as $line)
{
- if (($cols = str_getcsv($line, $splitchar, '"')) && count($schema) == count($cols))
- $records[] = (object)array_combine($schema, $cols);
+ if (($cols = str_getcsv($line, $splitchar, '"')) && (count($schema) == count($cols) || $nullcols))
+ $records[] = (object)array_combine($schema, $cols + $nullcols);
else
return (array)it::error((array)$p['it_error'] + ['title' => "csv column count mismatch: " . count($schema) . " in header vs " . count($cols) . " in row", 'body' => [$schema, $cols]]);
}