I am importing a CSV file that has the following format:
5021 2510 0600 1234 560,50,SLM
5021 2510 0600 1234 570,50,SLM
5021 2510 0600 1234 580,50,SLM
I am able to echo the data out to a 3 column table, after removing the spaces. What I am trying to do is extract from the first column number "5021251006001234560", just the segment "123456", and then append the "123456" to col[3] of the array of CSV rows. I think my comments in the code might help to explain what I am trying to achieve.
I guess what I'm after is a multidimensional array from the CSV file that looks like this:
5021251006001234560,50,SLM,123456
5021251006001234570,50,SLM,123457
5021251006001234580,50,SLM,123458
So that I can access the 4 values row by row. Here is my code so far:
$csv = array();
if (($file = fopen("uploads/" . $_FILES["file"]["name"], 'r')) === false)
{
throw new Exception('There was an error loading the CSV file.');
}
else
{
while (($line = fgetcsv($file, 1000)) !== false)
{
$csv[] = $line;
}
fclose($handle);
}
echo "<table>";
foreach ($csv as $rows => $row)
{
echo "<tr>";
foreach ($row as $col => $cell)
{
$serial = '000000';
$cell = preg_replace('/\s+/', '', $cell);
// Check if the $cell value contains "50212510" at the start
$findme = "50212510";
if (strpos($cell, $findme) === true) {
// Get the 6 digits before the last digit
$serial = substr($cell, 11, 6);
// And append the 6 digits as a forth[3] value to each CSV row
array_push($row, $serial);
}
echo "<td>" . $cell . "</td>";
if ($cell === "SLM") {
echo "<td>" . $serial ."</td>";
}
}
echo "</tr>";
}
echo "<table>";
Any assistance would be appreciated.