我需要检查csv文件的第一行,看它是否包含列的标题(数字,名称,数量)。如果第一行包含这些标题,则退出文件或添加数字,名称,金额,如果第一行不包含此信息。
我试图检查第一列,第一行是否包含一个表示没有标题的数字。
这是我正在使用的代码。再次感谢您的帮助。
$rows = $phones = [];
if (($handle = fopen($file_tmp, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
list($phone, $name, $amount) = $data;
if(!($data = fgetcsv($handle))) {
return; //most likely empty file
}
if(!is_numeric($data[0])) {
fputcsv($handle, array('number', 'name', 'amount'));
}
$phone = str_replace(['(',')','-'], '', $phone);
$amount = str_replace(['$'], "", $amount);
$amount = sprintf('$%.2f', $amount);
// track unique phone numbers here
if (isset($phones[$phone])) {
// it's a duplicate so just ignore the entire row
continue;
}
// otherwise it's a new phone number so store it
$phones[$phone] = true;
$rows[] = [$phone, $name, $amount];
// or output directly
//echo "$phone | $name | $amount";
}
fclose($handle);
}
答案 0 :(得分:0)
您有两种选择:
amount
字段的格式,您可以检查值是否对任何行有效用PHP解析“$ 100.00”到数字: