2011-04-08 51 views
1

我需要獲取數組中所有重複記錄的最後條目。我怎樣才能做到這一點在PHPPHP array所有重複記錄的最後條目

例如數據

Input Array 
Array 
(
    [0] => Array ([0] => A) 
    [1] => Array ([0] => A) 
    [2] => Array ([0] => B) 
    [3] => Array ([0] => C) 
    [4] => Array ([0] => C) 
    [5] => Array ([0] => D) 
    [6] => Array ([0] => F) 
    [7] => Array ([0] => F) 
) 

結果輸出表1只應包含的所有重複記錄

Array 
(
    [1] => Array ([0] => A) 
    [4] => Array ([0] => C) 
    [7] => Array ([0] => F) 
) 

結果輸出表2最後一個條目應包含所有其他enteries。

Array 
(
    [0] => Array ([0] => A) 
    [2] => Array ([0] => B) 
    [3] => Array ([0] => C) 
    [5] => Array ([0] => D) 
    [6] => Array ([0] => F) 
) 
+2

您是否試過編碼?你有錯誤嗎?發佈一些嘗試... – Khez 2011-04-08 09:58:15

+0

爲什麼你有這個......怪異的......結構? – 2011-04-08 10:01:53

+1

這是示例數據。真實的數據是不同的,但邏輯將是相同的。我正在閱讀csv文件。我需要一個小邏輯在這裏,如何讀取數組中的最後一個重複條目 – Developer 2011-04-08 10:04:26

回答

0

//我的實現是這樣。

set_time_limit (1500) ; 
ini_set("memory_limit","128M"); 

$fileName = "_one"; 

$objScan = new scanCSV(); 

$objScan->setCSVFileName($fileName); 
$objScan->loadCsvFile(); 
$objScan->separateDuplicateFromUniq(); 

$objScan->process(); 



class scanCSV 
{ 
    private $_csvFile = NULL; 
    private $_arrayListAll = NULL; 
    private $_allDuplicateRec = NULL; 
    private $_uniqueRec = NULL; 

    function setCSVFileName($fileName){ 
     $this->_csvFile = $fileName; 
    } 


    //----------------------------------------------------------------------- 
    function loadCsvFile() 
    { 
     $arrayListAll = array(); 
     if (($handle = fopen($this->_csvFile . ".csv", "r")) !== FALSE) { 
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
       $arrayListAll[] = $data; 
      } 
     } 

     $this->_arrayListAll = $arrayListAll; 
    } 
    //----------------------------------------------------------------------- 
    public function separateDuplicateFromUniq(){ 

     $allDuplicateRec = array(); 
     $uniqueRec = array(); 

     foreach($this->_arrayListAll as $data){ 
      if ($this->getcount($this->_arrayListAll, $data) > 1) 
        $allDuplicateRec[] = $data; 
      else 
        $uniqueRec[] = $data; 
     } 

     $this->_allDuplicateRec = $allDuplicateRec; 
     $this->_uniqueRec = $uniqueRec; 

    } 
    //----------------------------------------------------------------------- 
    public function process(){  
     $uniq = $this->removeDuplicate ($this->_allDuplicateRec); 
     $this->writeCSVFile ($this->_csvFile . "A.csv", $uniq); 

     $restofEntries = $this->removeLastEntries ($this->_arrayListAll, $uniq); 
     $this->writeCSVFile ($this->_csvFile . "B.csv", $restofEntries); 
    } 
    //----------------------------------------------------------------------- 
    function removeDuplicate ($allDuplicateRec) 
    { 

     foreach ($allDuplicateRec as $k => $v) 
     if ($this->getcount($allDuplicateRec, $v) > 1) 
      unset($allDuplicateRec[$k]); 
     return $allDuplicateRec; 
    } 

    //----------------------------------------------------------------------- 
    function removeLastEntries ($arrayListAll, $uniq){ 
     foreach ($uniq as $entry) 
      if(in_array($entry, $arrayListAll)) 
       unset($arrayListAll[array_search($entry, $arrayListAll)]); 

     return $arrayListAll; 

    } 
    //----------------------------------------------------------------------- 
    function getcount($arrayList1, $data){ 
      $address = $data[2]; 
      $count =0; 
      foreach ($arrayList1 as $dt) 
       if ($address == $dt[2]) 
        $count++; 

      return $count; 
    } 
    //----------------------------------------------------------------------- 
    function writeCSVFile ($fileName, $data){ 

     $fp = fopen($fileName, 'w'); 

     foreach ($data as $k=>$fields) 
      fputcsv($fp, $fields); 

     fclose($fp); 
    } 
    //----------------------------------------------------------------------- 
} // end of scan Optimized 
1

很快打出來。還沒有機會測試它,但應該做的伎倆。

請注意,內循環是非最優的 - 它可以提高很多,特別是如果數據已知被排序(如在示例數據中),但是您沒有指定,所以我避難沒有假設它。

<?php 
$output_dups = array(); 
$output_remainder = array(); 
foreach($input_array as $key=>$data) { 
    $dup_found = false; 
    foreach($input_array as $key2=>$data2) { 
     if($key < $key2 && $data===$data2) { $dup_found = true; } 
    } 
    if($dup_found) { $output_dups[] = $data; } else { $output_remainder[] = $data; } 
} 

?> 
+0

感謝斯普德利+1 – Developer 2011-04-08 13:22:52

2

不知道,如果你想這樣說,但你可以試試這個:

<?php 

$array = array 
(
    0 => array (0 => 'A'), 
    1 => array (0 => 'A'), 
    2 => array (0 => 'B'), 
    3 => array (0 => 'C'), 
    4 => array (0 => 'C'), 
    5 => array (0 => 'D'), 
    6 => array (0 => 'F'), 
    7 => array (0 => 'F') 
); 

foreach ($array as $k => $v){ 
    echo $k . " --> "; 
    if (is_array($v)){ 
     foreach($v as $k1=>$v1){ 
      echo $v1."<br />"; 
      $new_array[$k]=$v1; 
     } 
    }else{ 
     echo $v."<br />"; 
    } 
} 
echo "<br />"; 
// Restructured array 
print_r($new_array); 
echo "<br />"; 
// Duplicates 
print_r(get_duplicates($new_array)); 
echo "<br />"; 
// All entries 
print_r(array_unique($new_array)); 

// Function to get duplicates 
function get_duplicates($array) { 
    return array_unique(array_diff_assoc($array, array_unique($array))); 
} 
?> 

輸出爲:

//original 
Array ([0] => A [1] => A [2] => B [3] => C [4] => C [5] => D [6] => F [7] => F) 
//dupes 
Array ([1] => A [4] => C [7] => F) 
//all entries 
Array ([0] => A [2] => B [3] => C [5] => D [6] => F) 
+0

感謝Osh Mansor +1 – Developer 2011-04-08 13:23:24