查找不匹配的多维数组值

时间:2018-12-07 14:08:51

标签: php arrays multidimensional-array

我有两个多维数组。这两个数组几乎相同,只是其中一个数组的值在另一个数组中不存在。我正在使用的数组是:

Array1

Array
(
    [0] => Array
        (
            [dexcode] => BeS712210
        )

    [1] => Array
        (
            [dexcode] => De 48245
        )

    [2] => Array
        (
            [dexcode] => All50481
        )

    [3] => Array
        (
            [dexcode] => Bis1016338
        )

    [4] => Array
        (
            [dexcode] => S-T41471
        )

    [5] => Array
        (
            [dexcode] => Ron35844
        )

    [6] => Array
        (
            [dexcode] => Lin10961
        )

    [7] => Array
        (
            [dexcode] => Tip295926
        )

    [9] => Array
        (
            [dexcode] => SLN893827
        )

    [10] => Array
        (
            [dexcode] => Fen1016241
        )

    [11] => Array
        (
            [dexcode] => Aut331661
        )

    [12] => Array
        (
            [dexcode] => Pro39613
        )

    [13] => Array
        (
            [dexcode] => Com920158
        )

    [14] => Array
        (
            [dexcode] => Sma21322
        )

    [15] => Array
        (
            [dexcode] => Bev121157
        )

    [16] => Array
        (
            [dexcode] => Cit3043324
        )

    [17] => Array
        (
            [dexcode] => Que2182100
        )

)

Array2:

Array
(
    [0] => Array
        (
            [dexcode] => BeS712210
        )

    [1] => Array
        (
            [dexcode] => De 48245
        )

    [2] => Array
        (
            [dexcode] => All50481
        )

    [3] => Array
        (
            [dexcode] => Bis1016338
        )

    [4] => Array
        (
            [dexcode] => S-T41471
        )

    [5] => Array
        (
            [dexcode] => Ron35844
        )

    [6] => Array
        (
            [dexcode] => Lin10961
        )

    [7] => Array
        (
            [dexcode] => Tip295926
        )

    [9] => Array
        (
            [dexcode] => SLN893827
        )

    [10] => Array
        (
            [dexcode] => Fen1016241
        )

    [11] => Array
        (
            [dexcode] => Aut331661
        )

    [12] => Array
        (
            [dexcode] => Pro39613
        )

    [13] => Array
        (
            [dexcode] => Com920158
        )

    [14] => Array
        (
            [dexcode] => Sma21322
        )

    [15] => Array
        (
            [dexcode] => Bev121157
        )

    [16] => Array
        (
            [dexcode] => Que2182100
        )

)

不匹配的值:

[16] => Array
    (
        [dexcode] => Cit3043324
    )

使用PHP,我试图获得不匹配的值,如下所示:

$a1 =array_unique($all_codes,SORT_REGULAR);

$a2 =array_unique($matches,SORT_REGULAR);

$results = check_diff_multi($a1, $a2);

function check_diff_multi($array1, $array2){
    $results = array();
    foreach($array1 as $key => $val) {
         if(isset($array2[$key])){
           if(is_array($val) && $array2[$key]){
               $result[$key] = check_diff_multi($val, $array2[$key]);
           }
       } else {
           $results[$key] = $val;
       }
    }

    return array_unique($results,SORT_REGULAR);
}

$ a1是我上面发布的第一个包含不同值的数组。 $ a2是第二个。

应将不同的值添加到$ results变量中。

当我检查$结果中的内容时,是这样的:

print '<pre>';
print_r($results);
print '</pre>'; 

我得到这个结果:

Array
(
    [17] => Array
        (
            [dexcode] => Que2182100
        )

)

似乎只选择了最后一个值。有人知道为什么会这样吗?

1 个答案:

答案 0 :(得分:2)

使用array_columnarray_diff函数

const iReturnPromise = () => {
    return new Promise((resolve, reject) => {
        console.log("run");
        setTimeout(reject, 5);
    });
}

async function displayPromiseResults() {

    try {
        await iReturnPromise();
        console.log('!then');
    }

    catch(ex) {
        console.log("cought!");
    }

}

displayPromiseResults();

demo