tags:

views:

56

answers:

3

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?

A: 

http://php.net/manual/en/ref.array.php has plenty of functions for you.
array_intersect() or array_diff() for example

Manual pages are always better choice for such a direct questions.

Col. Shrapnel
+4  A: 

array_diff seems to be what you're looking for.

$nonattendees = array_diff($friends, $attendees);
MiffTheFox
A: 
// differancee items code 
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

print_r($result);

// common items code //

$array1 = array("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$result = array_intersect($array1, $array2);
print_r($result);
sam