array_diff
-
We can find the difference between two arrays using the
Enumerable#rejectmethod. This method rebuilds an array based on conditions that are evaluated in a block. In the block we simply reject any element of the array that is in the comparison array.PHP
$animals1 = array('cat', 'dog', 'bat', 'rat'); $animals2 = array('cat', 'dog'); $result = array_diff($animals1, $animals2); var_export($result); // => array(2 => 'bat', 3 => 'rat')
Ruby
animals1 = ['cat', 'dog', 'bat', 'rat'] animals2 = ['cat', 'dog'] p animals1.reject {|a| animals2.include?(a) } # => ["bat", "rat"]
see also
Looking for Rails or PHP web application development, integration, and training?
Rails for PHP is brought to you by Maintainable Software. Get custom web applications and personalized training from the authors of the book and website.

