检查多维数组中的重复结果

时间:2017-02-12 18:22:42

标签: php arrays multidimensional-array duplicates

我有一个多维数组,我需要交叉检查它是否有重复的结果,如果它们是重复的,请删除具有重复结果的数组。我需要检查3个密钥才能复制;号码,出发和到达。这是一个示例数组,其中第3个数组是重复的,需要删除:

[Cars] => Array
    (
        [0] => Array
            (
                [cartype] => car1
                [number] => 123
                [craft] => 456
                [departure] => GHY
                [departtime] => 20:25
                [arrival] => PUI
                [arrivetime] => 22:50
            )

        [1] => Array
            (
                [cartype] => car2
                [number] => 567
                [craft] => 890
                [departure] => LHY
                [departtime] => 16:25
                [arrival] => PGY
                [arrivetime] => 23:50
            )
        [2] => Array
            (
                [cartype] => car2
                [number] => 567
                [craft] => 890
                [departure] => LHY
                [departtime] => 16:25
                [arrival] => PGY
                [arrivetime] => 23:50
            )

    )

我真的很感激一些帮助。

谢谢,

2 个答案:

答案 0 :(得分:1)

使用in_array函数和自定义$hash_map 容器的解决方案(每个条目由numberdeparturearrival组成键值):

// $arr is your initial array

// hash container
$hash_map = [];
foreach ($arr['Cars'] as $k => &$v) {
    $hash = $v['number'] . $v['departure'] . $v['arrival'];
    if (in_array($hash, $hash_map)) {
        unset($arr['Cars'][$k]);   // removing duplicate item     
    } else {
        $hash_map[] = $hash;    
    }
}
unset($hash_map);

print_r($arr);

答案 1 :(得分:0)

尝试以下操作以完全删除相同的重复子阵列。

$arr = [['cartype' => 'car1','number' => '123','departure' => 'GHY','arrival' => 'PUI'],
        ['cartype' => 'car2','number' => '567','departure' => 'LHY','arrival' => 'PGY'],
        ['cartype' => 'car2','number' => '567','departure' => 'LHY','arrival' => 'PGY']];
$out = [];  
while (list($k, $v) = each($arr)) {
if(!in_array($v,$out)) {
    array_push($out,$v);
}
}
print_r($out);
// output
Array
(
    [0] => Array
        (
            [cartype] => car1
            [number] => 123
            [departure] => GHY
            [arrival] => PUI
        )

    [1] => Array
        (
            [cartype] => car2
            [number] => 567
            [departure] => LHY
            [arrival] => PGY
        )
)

查看示例here

修改 如果您只想用于检查重复结果。

$arr = [['cartype' => 'car1','number' => '123','departure' => 'GHY','arrival' => 'PUI'],
        ['cartype' => 'car2','number' => '567','departure' => 'LHY','arrival' => 'PGY'],
        ['cartype' => 'car2','number' => '567','departure' => 'LHY','arrival' => 'PGY']];
$out = []; 
$checks = [];
while (list($k, $v) = each($arr)) {
if(!in_array([$v['number'],$v['departure'],$v['arrival']],$checks)) {
    array_push($checks,[$v['number'],$v['departure'],$v['arrival']]);
    array_push($out,$v);
}
}
print_r($out);
// output 
Array
(
    [0] => Array
        (
            [cartype] => car1
            [number] => 123
            [departure] => GHY
            [arrival] => PUI
        )

    [1] => Array
        (
            [cartype] => car2
            [number] => 567
            [departure] => LHY
            [arrival] => PGY
        )
)

检查新示例here