0

I want to use the same principe that in_array, but with a double array and only search in the first value. Let me explain :

$array_1 = [["1","aaa"],["2","bbb"],["3","aaa"],["4","ddd"]]

$array_2 = [["2","bbb"],["3","aaa"],["4","ddd"]]

Now i want the function return ["1", "aaa"] because this part is not in the second array. But I want to search difference only with the id (number 1, 2, 3 and 4), not with the text who can be similar, but never the id.

user3415011
  • 195
  • 1
  • 2
  • 11

1 Answers1

1

Just try with array_udiff function:

$array_1 = [["1", "aaa"], ["2", "bbb"], ["3", "aaa"], ["4", "ddd"]];
$array_2 = [["2", "bbb"], ["3", "aaa"], ["4", "ddd"]];

$output = array_udiff($array_1, $array_2, function($a, $b){
  if ($a[0] < $b[0]) {
    return -1;
  } else if ($a[0] > $b[0]) {
    return 1;
  }
  return 0;
});

var_dump($output);

Output:

array (size=1)
  0 => 
    array (size=2)
      0 => string '1' (length=1)
      1 => string 'aaa' (length=3)
hsz
  • 148,279
  • 62
  • 259
  • 315