note 18894 added to function.array-diff

From: Date: Fri, 08 Feb 2002 17:03:46 +0000
Subject: note 18894 added to function.array-diff
Groups: php.notes 
Request: Send a blank email to php-notes+get-26390@lists.php.net to get a copy of this message
For those who are looking how to substract one array for another: you cannot use the "-" operator !!! But you can use this function: function array_minus_array($a, $b) { $c=array_diff($a,$b); $c=array_intersect($c, $a); return $c; } So, for example: $a[]="a"; $a[]="b"; $a[]="c"; $b[]="a"; $c=array_minus_array($a,$b); var_dump($c); Prints out: array(2) { [1]=> string(1) "b" [2]=> string(1) "c" } which is $a-$b, as we wanted. PD: you may also do a foreach ... if $a in_array $b ... but this is much more elegant !!! Credits to A.Atala "mandrake", who knows everything about mathematic. -- http://www.php.net/manual/en/function.array-diff.php http://master.php.net/manage/user-notes.php?action=edit+18894 http://master.php.net/manage/user-notes.php?action=delete+18894 http://master.php.net/manage/user-notes.php?action=reject+18894

« previous php.notes (#26390) next »