php two Multidimensional Array difference

时光怂恿深爱的人放手 提交于 2020-01-07 00:34:30

问题


I am working with two multidimensional array difference bellow are my array:

Array1:

Array
(
    [0] => Array
        (
            [F_CONTACT_ID] => 2
            [F_CONTACT_FNAME] => name2
            [F_CONTACT_NAME] => name22
        )

    [1] => Array
        (
            [F_CONTACT_ID] => 3
            [F_CONTACT_FNAME] => name3
            [F_CONTACT_NAME] => name33
        )

)

Array2:

Array
(
    [0] => Array
        (
            [F_CONTACT_ID] => 2
            [F_CONTACT_FNAME] => name2
            [F_CONTACT_NAME] => name22
        )

    [1] => Array
        (
            [F_CONTACT_ID] => 3
            [F_CONTACT_FNAME] => name3
            [F_CONTACT_NAME] => name33
        )

    [2] => Array
        (
            [F_CONTACT_ID] => 5
            [F_CONTACT_FNAME] => name5
            [F_CONTACT_NAME] => name55
        )

)

I just want o compare the difference with 'F_CONTACT_ID' in the array.

My Resulting Array Should be:

Result:

Array
(
    [2] => Array
        (
            [F_CONTACT_ID] => 5
            [F_CONTACT_FNAME] => name5
            [F_CONTACT_NAME] => name55
        )
)

Also If one array is empty: suppose Array2 is empty. My result Array should be:

Array
(
    [0] => Array
        (
            [F_CONTACT_ID] => 2
            [F_CONTACT_FNAME] => name2
            [F_CONTACT_NAME] => name22
        )

    [1] => Array
        (
            [F_CONTACT_ID] => 3
            [F_CONTACT_FNAME] => name3
            [F_CONTACT_NAME] => name33
        )

)

I tried with different solutions but nothing worked for me. I tried to retrieve the F_CONTACT_ID and stored in single-dimensional array and compare but It took lot of time. Kindly help me in better and fast solution.


回答1:


Have you tried this?

for($i=0;$i<count($array1);$i++) {
    $temp[$array1[$i]['F_CONTACT_ID']] = $array1[$i];
};

for($i=0;$i<count($array2);$i++) {
    if($temp[$array2[$i]['F_CONTACT_ID']]) {
        unset($temp[$array2[$i]['F_CONTACT_ID']]);
    } else {
        $temp[$array2[$i]['F_CONTACT_ID']] = $array2[$i];
    }
}

echo "<pre>";
print_r($temp);
echo "</pre>";

The result will be some thing like this:

Array
(
    [5] => Array
        (
            [F_CONTACT_ID] => 5
            [F_CONTACT_FNAME] => name5
            [F_CONTACT_NAME] => name55
        )

)



回答2:


You could try the function array-diff-key() function, which helps you to compare two multidimensional arrays using keys.

You could visit this page for more information: http://php.net/manual/en/function.array-diff-key.php




回答3:


what about something like this?

$array1;
$array2;

$array3;
foreach ($array1 as $ar1) {
    foreach ($array2 as $ar2) {
        if ($ar1['F_CONTACT_ID']==$ar2['F_CONTACT_ID']) {
            array_push($array3, $ar1);
        }
    }
}

it's not very optimize, to maximize performance change the second foreach checking if array_push is already append (using a bool condition)



来源:https://stackoverflow.com/questions/27856743/php-two-multidimensional-array-difference

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!