phparraysassociative-arrayarray-difference

Get the associative differences between two associative arrays


I have 2 arrays that I want to compare. On side A, it contains IPtables rules split up in separate columns. Example output array A;

Array
(
    [0] => Array
        (
            [num] => 1
            [pkts] => 0
            [bytes] => 0
            [target] => DROP
            [prot] => all
            [opt] => --
            [in] => *
            [out] => *
            [source] => 192.168.0.1/32
            [destination] => 192.168.0.2/32

        )

On side B, I also have an array. However, this one has a slight difference.

   [1] => Array
        (
            [num] => 2
            [pkts] => 0
            [bytes] => 0
            [target] => DROP
            [prot] => all
            [opt] => --
            [in] => *
            [out] => *
            [source] => 192.168.5.5/32
            [destination] => 192.168.6.6/32
            [id] => 7
        )

As you can see, Array B has another column. Column ID.

What I want to do is, compare these two from the eyes of A.

So,

A-->B then I want to output the difference.

In the field I have array A filled with 3 rules, and Array B for example 4 rules.

Array A needs to look at Array B, then output what is not in there.

Array B needs to be a exact copy of Array A, so to speak.

What I've tried, is to use array_diff. However, I've found out that that doesn't work since there's an ID column in array B, always being the difference.

So essentially what I'm looking for is a modified array_diff that doesn't look at the ID column in Array B... how to achieve this?


Solution

  • As already mentioned, you could either unset the id from the inner array via unset or write your own comparison function via array_diff_uassoc:

    http://www.php.net/manual/en/function.array-diff-uassoc.php