0
$a: Array
(
[161] => Array
    (
        [idAgent] => 30
        [idClient] => 97
    )

[163] => Array
    (
        [idAgent] => 30
        [idClient] => 107
    )

[712] => Array
    (
        [idAgent] => 12
        [idClient] => 349
    )

[718] => Array
    (
        [idAgent] => 12
        [idClient] => 57
    )

[721] => Array
    (
        [idAgent] => 9
        [idClient] => 236
    )

[729] => Array
    (
        [idAgent] => 12
        [idClient] => 118
    )

[739] => Array
    (
        [idAgent] => 20
        [idClient] => 483
    )
...
)

$b: Array
(
[160] => Array
    (
        [idAgent] => 31
        [idClient] => 926
    )

[162] => Array
    (
        [idAgent] => 30
        [idClient] => 97
    )
 ...
 )

I have two multidimensional arrays and I need to get the key whose array value belongs both $a and $b. In this case the result must be the array with 162 key.

$result: Array
(    
  [162] => Array
    (
        [idAgent] => 30
        [idClient] => 97
    )
...
)

I've tried, but without any luck, something like this:

array_intersect_uassoc( $a, $b, function ($A, $B){
    return ($A['idAgent'] - $B['idAgent']);
});

Or, it's better to create one multi array and then extract duplicates?

vmar
  • 13
  • 3
  • So only `idAgent` needs to match? – AbraCadaver Mar 01 '17 at 18:14
  • Possible duplicate of [How to search by key=>value in a multidimensional array in PHP](http://stackoverflow.com/questions/1019076/how-to-search-by-key-value-in-a-multidimensional-array-in-php) – miken32 Mar 01 '17 at 18:19

1 Answers1

1

You can use just array_uintersect (compares intersection of arrays by callback function).

The strcmp function in my example is just for easier comparison of strings.

If you need to search for unique combination of idAgent and idClient, put them as a string together for comparison.

$result = array_uintersect( $a, $b, function($A, $B){
    return strcmp($A['idAgent'] . '_' . $A['idClient'], $B['idAgent'] . '_' . $B['idClient']);
});
Petr Hejda
  • 40,554
  • 8
  • 72
  • 100
  • @CasimiretHippolyte that wouldn't be equal values (to the `idAgent` and `idClient` properties) as the OP has asked, so the function wouldn't see them as intersection. – Petr Hejda Mar 01 '17 at 20:08
  • @CasimiretHippolyte Oh I see. Updated my answer accordingly. – Petr Hejda Mar 01 '17 at 20:38
  • Correct, but I can't mark this as correct answer, because I do not have necessary reputation points. tx. – vmar Mar 02 '17 at 08:39