Finding common / unusual elements between two Arrays in PHP

advertisements

Consider I have two arrays:

$friends = Array('foo', 'bar', 'alpha');
$attendees = Array('foo', 'bar');

Now I need to populate a new array $nonattendees which contains only the elements which are in $friends array and not in $attendees array. i.e, $nonattendees array should be populated with 'alpha'.

Is there any in built array operation available in PHP to achieve the above functionality or should I write my own for loops?


array_diff seems to be what you're looking for.

$nonattendees = array_diff($friends, $attendees);