note 60985 added to function.array-diff

From: Date: Sun, 22 Jan 2006 18:40:20 +0000
Subject: note 60985 added to function.array-diff
Groups: php.notes 
Request: Send a blank email to php-notes+get-102244@lists.php.net to get a copy of this message
Sorry for the bug in my last comment (probably rightfully removed by the admins). If you want to compare more than 2 arrays, or don't know how many arrays need to be compared, this is your function: <?php # An extention to array_diff: # It returns an array of all values not present in all arrays given. If '$strict' is true, # it returns all values not present or not in the same order in all arrays given. The # arrays to compare must be placed in another array, which is used as argument '$arrays'. # Returns false if the '$arrays' is invalid. function array_rdiff ($arrays, $strict = false) { # check if argument is valid. if (!is_array ($arrays)) return false; foreach ($arrays as $array) if (!is_array ($array)) return false; # set working variables $diff = array (); $amount = count ($arrays); $needles = array_shift ($arrays); # compare for ($n = 0; $n < $amount; $n++) { for ($m = 0; $needles[$m]; $m++) { $found = true; $positions = array ($m); foreach ($arrays as $haystack) { if (($pos = array_search ($needles[$m], $haystack)) === false) $found = false; if ($strict) $positions[] = $pos; } if (!$found) $diff[] = $needle; elseif ($strict && (count (array_unique ($positions)) > 1)) $diff[] = $needle; } $arrays[] = $needles; $needles = array_shift ($arrays); } return array_unique ($diff); } ?> ---- Server IP: 83.137.20.120 Probable Submitter: 82.170.215.175 ---- Manual Page -- http://www.php.net/manual/en/function.array-diff.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+60985 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60985&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60985&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60985&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60985&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60985&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60985&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60985&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+60985&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#102244) next »