获取阵列之间的差异

my mind is going to explode. need to get the array element which has an id of 4 and 6 in array 1. basically its the difference of these two arrays. i tried using for each loop but i cant get the output that i want

Array 1(
 0 => array(
        id: 1
          )
 1 => array(
        id: 4
          )
 2 => array(
        id: 5
          )
 3 => array(
        id: 6
          )
 )

Array 2(

 0 => array(
        id: 1
          )
 1 => array(
        id: 5
          )
      )

the output i want:

new Array (
 0 => array (
      id: 4
            )
 1 => array (
      id: 6)
        )

what i've tried:

foreach ($array1 as $key => $value) {
    foreach ($array2 as $key2 => $value2) {
      if($value2['id']  !== $value['id']){
        $result['id'] = $value2['id'];
      }
    }
  }

Use array_diff_key to get your expected output.

array_diff_key($array1, $array2)

It compares the keys from array1 against the keys from array2 and returns the difference. This function is like array_diff() except the comparison is done on the keys instead of the values.

You can use array_udiff() function like this.

function diffCompare($a, $b)
{
    return $a['id'] - $b['id'];
}

$difference = array_udiff($array1, $array2, 'diffCompare');

ANSWER:

foreach ($array1 as $key => $value) {
   foreach ($array2 as $key2 => $value2) {
     if($value2['id']  !== $value['id']){
        unset($array1[$key2]);
      }
}
}

Explanation: array1 got more array element than array2, my problem here is i need to get the elements in array 1 that is not existing in array2. so to solve this, if array2 element is existing in array1, unset the element in array1 that is the same in array2. array1 will return the elements which is not existing in array2