在数组PHP中取消设置一个重复值

时间:2014-08-06 09:04:06

标签: php arrays

我有2个数组:Array1和Array2。正如您在Array1中看到的,我有2个重复值。所以我想要做的是取消其中一个dublicates(并不重要),因此我需要使用与Array1中未设置的值相同的键来取消Array2的值

Array1
(
    [0] => 1331-14-2-45
    [1] => 1344-1-4-22
    **[2] => 1409-1-1-4**
    [4] => 1312-14-1-23
    **[5] => 1409-1-1-4**
    [6] => 1365-10-3-30
)

Array2
(
    [0] => opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6
    [1] => opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6
    [2] => opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6
    [4] => deviceNotActive#nemodel.GPON.4.6
    [5] => deviceNotActive#nemodel.GPON.4.6
    [6] => opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6
)

3 个答案:

答案 0 :(得分:0)

您可以使用array_unique()功能,也可以查看Array functions list

答案 1 :(得分:0)

使用array_unique功能和array_diff

$uniqueValues = array_unique($inputArray);               //your  first array
$deletedValues = array_diff($inputArray, $uniqueValues);

foreach($deletedValues as $key => $deletedValue){
     unset($secondIput[$key]);                           //you second array
}

答案 2 :(得分:0)

<?php
$array1 = array
(
    0 => '1331-14-2-45',
    1 => '1344-1-4-22',
    2 => '1409-1-1-4',
    4 => '1312-14-1-23',
    5 => '1409-1-1-4',
    6 => '1365-10-3-30',
);
$array1_tmp = $array1;
$array2 = array
(
    0 => 'opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6',
    1 => 'opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6',
    2 => 'opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6',
    4 => 'deviceNotActive#nemodel.GPON.4.6',
    5 => 'deviceNotActive#nemodel.GPON.4.6',
    6 => 'opticalSignalLevelTooLow1490nm#nemodel.GPON.4.6',
);
$array1 = array_unique($array1);
$remove_keys = array_keys(array_diff_key($array1_tmp, $array1));
foreach($remove_keys as $k => $v) {
    unset($array2[$v]);
}