[英]Extract part string from csvRow[0] and append to csvRow[3]
我正在導入具有以下格式的CSV文件:
5021 2510 0600 1234 560,50,SLM
5021 2510 0600 1234 570,50,SLM
5021 2510 0600 1234 580,50,SLM
刪除空格后,我能夠將數據回顯到3列表中。 我想做的是從第一列號“ 5021251006001234560”中提取出片段“ 123456”,然后將“ 123456”附加到CSV行數組的col [3]中。 我認為代碼中的注釋可能有助於解釋我要實現的目標。
我想我想要的是來自CSV文件的多維數組,如下所示:
5021251006001234560,50,SLM,123456
5021251006001234570,50,SLM,123457
5021251006001234580,50,SLM,123458
這樣我就可以逐行訪問4個值。 到目前為止,這是我的代碼:
$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>";
任何援助將不勝感激。
我有答案。 如此多次,闡明問題,實現了答案。
foreach($array as $line)
{
foreach( $line as $cell )
{
if (substr($cell, 0, 8) === "50212510") {
$serial = substr($cell, -7, 6);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.