在数组中查找非重复项

时间:2014-09-04 05:42:39

标签: php arrays

我有两个从不同目录构建的数组,其中包含剥离扩展名的文件名。我想找到那些没有成对的,因此我合并了数组以获得下面的数组。如何在数组中找到唯一的 重复项?

Array
(
    [0] => dbbackup_2014.09.03_07_06_27
    [1] => dbbackup_2014.09.03_07_07_08
    [2] => dbbackup_2014.09.03_07_13_33
    [3] => dbbackup_2014.09.03_07_15_24
    [4] => dbbackup_2014.09.03_07_21_57
    [5] => dbbackup_2014.09.03_07_22_11
    [6] => dbbackup_2014.09.03_08_40_35
    [7] => dbbackup_2014.09.03_08_41_36
    [8] => dbbackup_2014.09.03_08_43_38
    [9] => dbbackup_2014.09.04_04_59_08
    [10] => dbbackup_2014.09.03_07_06_27
    [11] => dbbackup_2014.09.03_07_07_08
    [12] => dbbackup_2014.09.03_07_13_33
    [13] => dbbackup_2014.09.03_07_15_24
    [14] => dbbackup_2014.09.03_07_21_57
    [15] => dbbackup_2014.09.03_07_22_11
    [16] => dbbackup_2014.09.03_08_40_35
    [17] => dbbackup_2014.09.03_08_41_36
    [18] => dbbackup_2014.09.03_08_43_38
)

注意:它是[9]

2 个答案:

答案 0 :(得分:2)

$a = array_flip(array_filter(array_count_values($a),function($item){
    return $item == 1 ? true : false;
}));
print_r($a);

输出

Array
(
    [1] => dbbackup_2014.09.04_04_59_08
)

Ideone

答案 1 :(得分:0)

foreach($array as $data)
{
    $values=explode("_",$data);
    $output[$values[1]]++;
}

foreach($output as $date=>$number)
{
     if($number==1)
     echo $date; 
}

<强>输出:

2014.09.04

<强> Fiddle