循环不清除以前的数组变量?

时间:2015-09-24 13:28:13

标签: php loops preg-match-all

由于某些原因,在运行我的脚本时,如果$ addr没有返回任何内容,则只使用之前不正确的结果。我需要它只是插入数据库空白。这是一个发生什么的例子

Insert 12345678
inserted wo 12345678

Insert 12222222
Insert 123 test st
inserted wo 12222222
inserted addr 123 test st

Insert 44444444
inserted wo 44444444
inserted addr 123 test st

Insert 555555555
inserted wo 555555555
inserted addr 123 test st

以下是我的代码:

$handle = fopen("./dumps/omega.txt", "r");
if ($handle) {
    while (($line = fgets($handle)) !== false) {
        // process the line read.
        echo $line;
        echo "<br>";

// Getting Address
    $ptn2 = "/\d+[ ](?:[A-Za-z0-9-]+[ ]?)+/";
    preg_match_all($ptn2, $line, $matches2, PREG_SET_ORDER);

// Getting WO
    $ptn = "/[0-9]{8}/";
    preg_match_all($ptn, $line, $matches, PREG_SET_ORDER);


// Printing
    //Address
    foreach($matches2 as $match2) { echo "Addr: " . $match2[0];
        echo "<br>"; }
        // WO
        foreach($matches as $match) { echo "WO: " . $match[0];
        echo "<br>"; }
        echo "<br>";
        echo "<hr>";

// Checking Import Status

    $wo = $match[0];
    $addr = $match2[0];

    $checkwo = "SELECT * FROM completed WHERE wo = '" .mysql_real_escape_string($wo). "'";
    $checkwo = mysql_query($checkwo) or die(mysql_error());

if(mysql_num_rows($checkwo) == 0) {
     // row not found, do stuff...
     echo "<b>IMPORTING!</b>";
     echo "<br>";
     echo $wo;
     echo "<br>";
     echo $addr;
     echo "<br>";

     //SQL Import
     $sqlinsert = "INSERT INTO completed (wo, addr) VALUES ('".$wo."', '".$addr."')";
     //mysql_query($sqlinsert) or die(mysql_error());


} else {
    // do other stuff...
    echo "Already exists...";

}
        echo "<br>";
        echo "<hr>";
        echo "<hr>";

    }

    fclose($handle);
} else {
    // error opening the file.
    echo "error";
} 

我已通过在循环中添加以下内容来纠正此问题

unset( $match[0] );
unset( $match2[0] );

0 个答案:

没有答案