note 20599 added to function.array-diff

From: Date: Tue, 09 Apr 2002 15:53:42 +0000
Subject: note 20599 added to function.array-diff
Groups: php.notes 
Request: Send a blank email to php-notes+get-29018@lists.php.net to get a copy of this message
In PHP 4.0.6 the array_minus_array() as shown above didn't work properly, especially when you use duplicated values: When I wanted to substract 'Array(1,1,2,3)' with 'Array (1,2,4)' I wanted as result: 'Array(1,3)' And not just 'Array (3)' as it would give using array_diff(), or nothing as result using the above array_minus_array function. This worked for me: function array_minus_array($a,$b) { $c = Array(); foreach ($a as $key => $val) { $posb = array_search($val,$b); if (is_integer($posb)) { unset($b[$posb]); } else { $c[] = $val; } } return $c; } -- http://www.php.net/manual/en/function.array-diff.php http://master.php.net/manage/user-notes.php?action=edit+20599 http://master.php.net/manage/user-notes.php?action=delete+20599 http://master.php.net/manage/user-notes.php?action=reject+20599

« previous php.notes (#29018) next »