Getting unique values from 2 arrays

前端 未结 7 1951
借酒劲吻你
借酒劲吻你 2020-12-03 11:01

I have 2 arrays that I\'m trying to get the unique values only from them. So I\'m not just trying to remove duplicates, I\'m actually trying to remove both duplicates.

7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 11:26

    The other answers are on the right track, but array_diff only works in one direction -- ie. it returns the values that exist in the first array given that aren't in any others.

    What you want to do is get the difference in both directions and then merge the differences together:

    $array1 = array(10, 15, 20, 25);
    $array2 = array(10, 15, 100, 150);
    $output = array_merge(array_diff($array1, $array2), array_diff($array2, $array1));
    // $output will be (20, 25, 100, 150);
    

提交回复
热议问题