I'm importing data from a CSV via PHP.
On some lines of the CSV, column 1 has a value, and on other lines it does not have a value. Column 2 always has a value.
If column 1 is empty and column 2 has a value, I would like column 1 to use the previous value in column 1. For example
|-------|-------|
| Col 1 | Col 2 |
|-------|-------|
| A | 2 |
| B | 5 |
| C | 3 |
| | 1 |
| D | 7 |
Would return
A2
B5
C3
C1
D7
I can use prev() on an array, but as $data[0] is a string I can't use prev()
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if ($i > 0) {
if ($data[0]=='' && $data[1]!=='') {
echo prev($data[0]).$data[1]'<br/>';
} else {
echo $data[0].$data[1]'<br/>';
}
}
$i++;
}
Banging my head against a wall for hours now, so I'd love a pointer!