I’m converting a csv file to xml using the following function:
I want to repeat the same action if $records[$row] value is the same as the previous time that it looped through. How can I accomplish this?
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
$records[] = $data;
$stringData .= '<PayTo PTPayeeID="' . $records[$row][2];
// if the value of $records[$row][2] is the same I want to repeat an action.
echo "<div id=\"row\"><div id=\"num\">" .$row. "</div>";
$row++;
for ($c=0; $c < $num; $c++) {
if ($c != 1) {
echo "<div class=\"field\">" . $data[$c] . "</div>";
}
}
}
In general, to compare a row with the previous one, store the previous row’s value in a variable.
Make sure to assign the variable with a value that won’t occur as a real value in your data.