0

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.

2

3 Answers 3

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
        )

)
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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)

2 Comments

Nope! I have gone through this long back.!
if u read the question again and check with ur solution u will know that.. if u explain me what will be the output of $array3 u will know it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.