2016-03-01 28 views
0

第一篇文章。一整天都在這裏尋找解決問題的方法。在if語句中使用爆炸數組

我使用的形式上傳的.txt與此格式的數組:

「VAR1」, 「VAR2」, 「VAR3」, 「VAR4」 ...等

這裏是我的代碼:

<?php 

$file = $_FILES['uploaded_file']['tmp_name']; 
$file2 = nl2br(file_get_contents($file)); 
$file3 = str_replace('"' , '' , $file2); 
$file4 = str_replace('ÿþ' , '' , $file3); 
$line_break = explode("<br />" , $file4); 
$topchange=105; 
    foreach ($line_break as $final1) { 

    $final2 = explode("," , $final1); 
    $regex = '#\((([^()]+|(?R))*)\)#'; 
    preg_match($regex, $final1, $match); 

     if($final2[4] == "DF") { 
      $type == "DF"; 
     } else { $type == "S"; } 

    echo "<div style='position:absolute;TOP:". $topchange .";LEFT:395;'>". $final2[1] ."</div>"; 
    echo "<div style='position:absolute;TOP:". $topchange .";LEFT:446;'>". $final2[3] ."</div>"; 
    echo "<div style='position:absolute;TOP:". $topchange .";LEFT:365;'>". $match[0] ."</div>"; 
    echo "<div style='position:absolute;TOP:". $topchange .";LEFT:335;'>". $type ."</div>"; 
    echo "<div style='position:absolute;TOP:". $topchange .";LEFT:520;'>". $final2[6] ."</div>"; 
     $changeamt = 24.2; 
     $topchange = $topchange + $changeamt; 



    } 


?> 

對不起,如果我的格式是可怕的,但它爲我工作。我的問題在於簡單的if語句。我回顯了$ final2 [4]以確保它輸出純文本。據我所知,頁面和頁面源,它​​是作爲各種小文本串(TE,WE,BE,P,S和DF)出現的。我基本上希望每個字符串都改爲S,除非它是DF,在這種情況下它將保持不變。

一直在這工作了一段時間,無法弄清楚。

回答

3

裏面你的,如果你必須分配和沒有比較,你的代碼是這樣的:

if($final2[4] == "DF") { 
    $type == "DF"; 
} else { 
    $type == "S"; 
} 

而且你應該這樣做:

$type = "S"; 
if($final2[4] == "DF") { 
    $type = "DF"; 
} 
+0

在這種情況下,你指定$ DF,但它可能有其他價值。正確的代碼是:$ df =($ final2 [4]!=='DF')? 'S':'DF'; – cpugourou