2

I am not able to compare Multi-Dimension Array:

Lets Say I have two array as follows!

Array 1:

Array
    (
        [0] => Array
            (
                [a] => 12XXYYZZ
                [b] => 30
                [c] => 12
            )

        [1] => Array
            (
                [a] => 12SSYYZZ
                [b] => 66
                [c] => 44
            )
        [2] => Array
            (
                [a] => 12EEYYZZ
                [b] => 66
                [c] => 56
            )

    )

Array 2:

Array
    (
        [0] => Array
            (
                [a] => 12XXYYZZ
                [b] => 30
                [c] => 12
            )

        [1] => Array
            (
                [a] => 12SSYYZZ
                [b] => 66
                [c] => 44
            )


    )

I am trying to compare this two array and find what are the sub array not present in array 2;

Note: I tried Array DIFF but not working for Multi-Dimension Array.

1

1 Answer 1

1

You can use serialize to provide comparison, for example:

$result = array_map(
   'unserialize', 
   array_diff(
      array_map('serialize', $one),
      array_map('serialize', $two)
));

-but note, that it this case keys values and their order will matter. Alternatively, you can use array_udiff like follows:

$result = array_udiff($one, $two, function($x, $y)
{
   return strcasecmp(serialize($x), serialize($y));
});
Sign up to request clarification or add additional context in comments.

Comments

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.