Array difference in PHP

Here is the code to check two arrays A and B and to find the difference of these arrays A-B, or to find the elements that exist only in array A and that don’t exist in array B.

$list1="
youtube_link
youtube_link_callback
youtube_sanitize_url
youtube_shortcode
you 
are 
super 
hero
";

$list2="
youtube_link
youtube_link_callback
youtube_sanitize_url
youtube_shortcode
zero
is 
no 
hero
";

// for all plugins
$a1 = explode("\n", $list1);
$a2 = explode("\n", $list2);
$diff=array();

foreach($a1 as $e){
 if(in_array($e, $a2)) continue; //with next
else
 $diff[] = $e;
}
print_r($diff);

Cannot be more simple than that? Well it can. There is array_diff function.

$diff = array_diff($a1, $a2);
print_r($diff);

The output is like this:

Array
(
    [0] => you 
    [1] => are 
    [2] => super 
)

tags: & category: -