简体   繁体   English

PHP用不同的列迭代CSV

[英]PHP Iterating CSV with different columns

I have a CSV defined like the data this. 我有一个像这样定义的CSV数据。

"PID",                 "FName",         "LName",       "Email"
2425751712402934017,
1037862,                "Jason",        "Van Hooser",   "jvanhooser@example.com"
961741,                 "Alana",        "Traxler",      "atraxler@example.com"
1100854,                "Emily",        "Walcheck",     "ewalcheck@example.com"
1166892,                "Mary",         "Thomas",       "mthomas@example.com"
8853065679823467777,
1179079,                "Donna",        "Thimm",        "dthimm@example.com"
927671,                 "Lillian",      "Wasson",       "lwasson@example.com"
1175139,                "Barry",        "Tollison",     "btollison@example.com"
1058086,                "Christina",    "Viktorin",     "cviktorin@example.com"

What I need to do is iterate through it and when it comes to the lines where there is only the PID field with the long number, I need to store that in a variable ($wkey) and then use it in an insert statement. 我需要做的是遍历它,当涉及到只有长数字的PID字段的行时,我需要将其存储在变量($ wkey)中,然后在insert语句中使用它。 I know we could put the value on each row but the process that outputs the file cannot do that. 我知道我们可以将值放在每一行上,但是输出文件的过程无法做到这一点。

Hwere is my code: 这是我的代码:

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    if($flag) {
        $flag = false;
        continue;
    }
    $import = "INSERT into exp_wb_bulk_reg(`WKey`,`PID`,`FName`,`LName`, `Email`,`status`) "
            . "values($wkey, '$data[0]','$data[1]','$data[2]','$data[3]','I')";
    // Use the sql to insert into the table
}        
fclose($handle);

How would I modify this to do what I need? 我将如何修改它来做我需要的事情?

Here's working code: 这是工作代码:

// skip header line
$data = fgetcsv($handle, 1000, ",");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    // get wkey for lines that has empty second column
    if(trim($data[1]) == "") {
        $wkey = $data[0];
    }
    if(trim($data[1]) != "") {
        $import = "INSERT into exp_wb_bulk_registrations(`WebinarKey`, `PID`,`FName`,`LName`, `Email`,`status`) "
                . "values($wkey, '$data[0]','$data[1]','$data[2]','$data[3]','I')";
        echo $import."<br />";
    }
}        
fclose($handle);

You could just check the size of $data array. 您可以只检查$data数组的大小。 Something like: 就像是:

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    if(count($data) == 4){
        $import = "INSERT into exp_wb_bulk_reg(`WKey`,`PID`,`FName`,`LName`, `Email`,`status`) "
            . "values($wkey, '$data[0]','$data[1]','$data[2]','$data[3]','I')";
        // Use the sql to insert into the table
    } else if(count($data) == ?) {
        //DO STUFF
    }
}        
fclose($handle);

Here's my variant: 这是我的变体:

$wkey = ''; // just for sure

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) 
{
    if(count($data)==1) {  // hey, we found a wKey! let's remember it
        $wkey = $data[0]; continue; 
    }

    $import = "INSERT into exp_wb_bulk_reg(`WKey`,`PID`,`FName`,`LName`, `Email`,`status`) "
            . "values($wkey, '$data[0]','$data[1]','$data[2]','$data[3]','I')";
}        
fclose($handle);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM