PHP数组所有重复记录的最后一个条目

时间:2011-04-08 09:56:17

标签: php arrays

我需要获取数组中所有重复记录的最后一个条目。我怎么能在php

中做到这一点

示例数据

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应包含所有其他企业。

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

3 个答案:

答案 0 :(得分: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 )

答案 1 :(得分: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; }
}

?>

答案 2 :(得分: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