2010-03-10 126 views

回答

0
class DataRow extends Object { 
    var $date; 
    var $offerid; 
    var $clicks; 
    var $orders; 
    var $shown; 
} 

function getMatchingRow($date, $offerid, $rows){ 
    foreach($rows as $myRow){ 
     if($myRow->date == $date && $myRow->offerid == $offerid){ 
      return $myRow; 
     } 
    } 
    return null; 
} 

$rows = array(); 
$complete_rows = array(); 

$first = file('FirstFile.csv'); 
$second = file('SecondFile.csv'); 

foreach($first as $line){ 
    $row = new DataRow(); 
    $parts = explode(',',$line); 
    $row->date = $parts[0]; 
    $row->offerid = $parts[1]; 
    $row->clicks = $parts[2]; 
    $row->orders = $parts[3]; 
    $rows[] = $row; 
} 

foreach($second as $line){ 
    $parts = explode(',',$line); 
    $row = getMatchingRow($parts[0],$parts[1],$rows); 
    if(!is_null($row){ 
     $row->shown = parts[2]; 
    } 
    $complete_rows[] = $row; 
} 

// loop over $completed_rows and write each to a new file 

未經測試,但那應該讓你大部分的方式。祝你好運。

+0

爆炸ona「,」不足以解析csv文件。如果任何數據中都有「,」,則表明您已被擰緊。 – 2010-03-10 23:29:38

0

在PHP中將這兩個文件讀入數組。

然後,做一個嵌套循環

for (all items in array A) { 
    find a matching item in array B 
    set properties from item from array B on the item in array A 
} 

其結果是,你必須有來自CSV文件的信息數組中。

+0

你能提供一個示例代碼來看看它是如何工作的。 – Rachel 2010-03-10 23:06:13