Intersection of Arrays in PHP

Hello1
Array
(
    [versions] => Array
        (
            [0] => Array
                (
                    [version] => 2020-10
                    [actions] => Array
                        (
                            [0] => Array
                                (
                                    [action] => action_OTA_Ping
                                )

                            [1] => Array
                                (
                                    [action] => action_OTA_HotelRatePlan_BaseRates
                                    [supports] => Array
                                        (
                                            [0] => OTA_HotelRatePlan_BaseRates_deltas
                                        )

                                )

                            [2] => Array
                                (
                                    [action] => action_OTA_HotelRatePlanNotif_RatePlans
                                    [supports] => Array
                                        (
                                            [0] => OTA_HotelRatePlanNotif_accept_overlay
                                            [1] => OTA_HotelRatePlanNotif_accept_Supplements
                                            [2] => OTA_HotelRatePlanNotif_accept_RatePlan_BookingRule
                                            [3] => OTA_HotelRatePlanNotif_accept_FreeNightsOffers
                                            [4] => OTA_HotelRatePlanNotif_accept_FamilyOffers
                                        )

                                )

                        )

                )

        )

)
Hello2
Array
(
    [versions] => Array
        (
            [0] => Array
                (
                    [version] => 2020-10
                    [actions] => Array
                        (
                            [0] => Array
                                (
                                    [action] => action_OTA_Ping
                                )

                            [1] => Array
                                (
                                    [action] => action_OTA_Read
                                )

                            [2] => Array
                                (
                                    [action] => action_OTA_HotelResNotif_GuestRequests
                                )

                            [3] => Array
                                (
                                    [action] => action_OTA_HotelRatePlan_BaseRates
                                )

                            [4] => Array
                                (
                                    [action] => action_OTA_HotelInvCountNotif
                                    [supports] => Array
                                        (
                                            [0] => OTA_HotelInvCountNotif_accept_categories
                                        )

                                )

                        )

                )

        )

)
intersect
Array
(
    [versions] => Array
        (
            [0] => Array
                (
                    [version] => 2020-10
                    [actions] => Array
                        (
                            [0] => Array
                                (
                                    [action] => action_OTA_Ping
                                )

                            [1] => Array
                                (
                                    [action] => action_OTA_HotelRatePlan_BaseRates
                                )

                            [2] => Array
                                (
                                    [supports] => Array
                                        (
                                        )

                                )

                        )

                )

        )

)
Hello1
Array
(
    [x] => Array
        (
            [0] => Array
                (
                    [0] => f
                    [g] => Array
                        (
                            [0] => 1
                        )

                    [y] => 22
                    [1] => z
                )

        )

    [y] => Array
        (
            [x] => 1
        )

    [0] => 1
    [1] => 2
    [2] => Array
        (
            [0] => a
            [1] => b
            [2] => c
            [3] => Array
                (
                    [0] => i
                    [1] => 1
                )

        )

)
Hello2
Array
(
    [x] => Array
        (
            [0] => Array
                (
                    [0] => f
                    [g] => Array
                        (
                            [0] => 1
                            [1] => 2
                            [2] => 3
                        )

                    [y] => 23
                    [1] => w
                )

        )

    [y] => Array
        (
            [x] => 2
        )

    [0] => 1
    [1] => Array
        (
            [0] => a
            [1] => b
            [2] => Array
                (
                    [0] => i
                    [1] => j
                )

        )

)
intersect
Array
(
    [x] => Array
        (
            [0] => Array
                (
                    [0] => f
                    [g] => Array
                        (
                            [0] => 1
                        )

                )

        )

    [y] => Array
        (
        )

    [0] => 1
    [2] => Array
        (
            [0] => a
            [1] => b
            [3] => Array
                (
                    [0] => i
                )

        )

)

Code

function recursive_array_intersect(array $array1, array $array2) { $array1 = array_intersect($array1, $array2); foreach ($array1 as $key1 => &$value) { if (is_int($key1)) { // If key is int we do not know which // entries should be matched so we have to find the one // with the biggest intersection // Very Ugly worst case n^dim $biggest_intersect = []; $biggest_match = 0; foreach ($array2 as $key2 => $value2) { if (is_int($key2)) { if (is_array($value) && is_array($value2)) { $new_intersect = recursive_array_intersect($value, $value2); $new_match = empty($new_intersect)?0:count(new_intersect, COUNT_RECURSIVE); if ($new_match > $biggest_match) { $biggest_intersect = $new_intersect; $biggest_match = $new_match; } } } } if ($biggest_match > 0) { $value = $biggest_intersect; } } else if (is_array($value) && is_array($array2[$key1])) { // If the Key is not an int we can match it $value = recursive_array_intersect($value, $array2[$key1]); } } return $array1; }