phparraysmultidimensional-arrayfilterarray-intersect

Get intersections of two multidimensional arrays


These are my 2 arrays:

Array
(
[25] => Array
    (
        [items_to_give] => Array
            (
                [0] => Array
                    (
                        [xx] => xx
                        [xx] => xx
                    )

            )

        [items_to_get] => Array
            (
                [0] => Array
                    (
                        [xx] => xx
                        [xx] => xx
                    )

            )

    )

[26] => Array
    (
        [items_to_give] => Array
            (
                [0] => Array
                    (
                        [xx] => xx
                        [xx] => xx
                    )

            )

        [items_to_get] => Array
            (
                [0] => Array
                    (
                        [xx] => xx
                        [xx] => xx
                    )

            )

    )

[36] => Array
    (
        [items_to_give] => Array
            (
                [0] => Array
                    (
                        [xx] => xx
                        [xx] => xx
                    )
            )

        [items_to_get] => Array
            (
               [0] => Array
                    (
                        [xx] => xx
                        [xx] => xx
                    )

            )

    )

)

and

Array
(
[25] => Array
    (
        [items_to_give] => Array
            (
                [0] => Array
                    (
                        [xx] => xx
                        [xx] => xx
                    )

            )

        [items_to_get] => Array
            (
                 [0] => Array
                    (
                        [xx] => xx
                        [xx] => xx
                    )

            )

    )

[26] => Array
    (
        [items_to_give] => Array
            (
                [0] => Array
                    (
                        [xx] => xx
                        [xx] => xx
                    )

            )

        [items_to_get] => Array
            (
                 [0] => Array
                    (
                        [xx] => xx
                        [xx] => xx
                    )

            )

    )

[30] => Array
    (
        [items_to_give] => Array
            (
                 [0] => Array
                    (
                        [xx] => xx
                        [xx] => xx
                    )

            )

        [items_to_get] => Array
            (
                 [0] => Array
                    (
                        [xx] => xx
                        [xx] => xx
                    )

            )

    )

)

And now i want to compare these arrays and return all arrayitems which are contained in both arrays. In the example these are [25] and [26].

So the returned array should look like this (As i said [25] and [26] are contained in both arrays so its returned):

Array
(
[25] => Array
    (
        [items_to_give] => Array
            (
                [0] => Array
                    (
                        [xx] => xx
                        [xx] => xx
                    )

            )

        [items_to_get] => Array
            (
                 [0] => Array
                    (
                        [xx] => xx
                        [xx] => xx
                    )

            )

    )

[26] => Array
    (
        [items_to_give] => Array
            (
                [0] => Array
                    (
                        [xx] => xx
                        [xx] => xx
                    )

            )

        [items_to_get] => Array
            (
                 [0] => Array
                    (
                        [xx] => xx
                        [xx] => xx
                    )

            )

    )
)

What i already tried:

$result = array_map("unserialize", array_intersect($this->serialize_array_values($array1),$this->serialize_array_values($array2)));

function serialize_array_values($arr){
    foreach($arr as $key=>$val){
        sort($val);
        $arr[$key]=serialize($val);
    }

    return $arr;
}

But this returns the array in the wrong format. The [items_to_give] and [items_to_get] are getting converted to the index [0] and [1] which is undesirable.

I want the array to stay as he is when returned that means i need [items_to_give] and [items_to_get] instead of indexes [0] and [1]

What are other possible solutions? Thanks for solutions.


Solution

  • Try this function:

    function my_array_diff($arr1, $arr2)
      {
        $res = array();
        foreach ($arr1 as $val1) {
          $val1 = serialize($val1);
          foreach ($arr2 as $key2 => $val2) {
            if (serialize($val2) == $val1) {
              $res[$key2] = $val2;
            }
          }
        }
    
        return $res;
      }