0

I need a solution to get the same value between two arrays with unknown index.

For example here the 1st array $a,

Array(
[0] => Array
    (
        [field_name] => Aerospace
        [zonegeo] => Angola
    )

[1] => Array
    (
        [field_name] => Aerospace
        [zonegeo] => Cameroon
    )

[2] => Array
    (
        [field_name] => Aerospace
        [zonegeo] => Congo
    )

[3] => Array
    (
        [field_name] => Beauty - Care
        [zonegeo] => Angola
    )

[4] => Array
    (
        [field_name] => Beauty - Care
        [zonegeo] => Cameroon
    )

[5] => Array
    (
        [field_name] => Beauty - Care
        [zonegeo] => Swaziland
    )
)

The 2nd array $b:

Array(
[0] => Array
    (
        [field_name] => Beauty - Care
        [zonegeo] => Angola
    )

[1] => Array
    (
        [field_name] => Beauty - Care
        [zonegeo] => Swaziland
    )
)

I would like to get a third array which contains common values of 1st and 2nd array.

Same values are

     1.[field_name] => Beauty - Care [zonegeo] => Angola 
     2.[field_name] => Beauty - Care [zonegeo] => Swaziland

I tried array_intersect($a, $b) but it doesn't work.

Cœur
  • 37,241
  • 25
  • 195
  • 267
yuhell
  • 15
  • 4
  • 2
    Did you check this one https://stackoverflow.com/questions/5653241/using-array-intersect-on-a-multi-dimensional-array – Ravinder Reddy Oct 26 '17 at 15:04
  • Yes, I saw it, there is an error : array_uintersect() expects parameter 3 to be a valid callback, function 'compareDeepValue' not found or invalid function name in [...] – yuhell Oct 26 '17 at 15:29
  • [Compare two 2D arrays & get intersection and differences](https://stackoverflow.com/q/37564953/2943403) – mickmackusa Feb 24 '22 at 05:03

2 Answers2

1

The problem is that array_intersect uses string comparison, and when the arrays are converted to strings, they're all equal, so they all end up in the intersection. (And you get a bunch of array to string conversion notices, which is not great either.) To compare the inner arrays as arrays instead, you can use array_uintersect, with a callback that compares the arrays.

$x = array_uintersect($a, $b, function($a, $b) {
    return $a <=> $b;
});

If you don't have PHP 7, then you can't use the <=> operator, so you'll need a few more lines to duplicate its functionality.

$x = array_uintersect($a, $b, function($a, $b) {
    if ($a < $b) return -1;
    if ($a > $b) return 1;
    return 0;
});
Don't Panic
  • 41,125
  • 10
  • 61
  • 80
0

Check this solution. Considering keys of both array are same/matching.

//Computes the intersection of arrays, compares data by a callback function
$intersect_array = array_uintersect($a, $b, 'compareArray');
print_r($intersect_array );

// call back function code
function compareArray($val1, $val2){
  // convert the array to string by serialize and compare the strings
  return strcmp(serialize($val1), serialize($val2));
}

Out put:

Array
(
    [3] => Array
        (
            [field_name] => Beauty - Care
            [zonegeo] => Angola
        )

    [5] => Array
        (
            [field_name] => Beauty - Care
            [zonegeo] => Swaziland
        )

)
Ravinder Reddy
  • 3,869
  • 1
  • 13
  • 22