note 107928 added to function.array-diff
| From: | merlyn dot tgz at gmail dot com | Date: | Thu, 15 Mar 2012 10:08:12 +0000 |
| Subject: | note 107928 added to function.array-diff | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186676@lists.php.net to get a copy of this message | ||
There is more fast implementation of array_diff, but with some limitations. If you need compare two
arrays of integers or strings you can use such function:
public static function arrayDiffEmulation($arrayFrom, $arrayAgainst)
{
$arrayAgainst = array_flip($arrayAgainst);
foreach ($arrayFrom as $key => $value) {
if(isset($arrayAgainst[$value])) {
unset($arrayFrom[$key]);
}
}
return $arrayFrom;
}
It is ~10x faster than array_diff
php > $t = microtime(true);$a = range(0,25000); $b = range(15000,500000); $c = array_diff($a,
$b);echo microtime(true) - $t;
4.4335179328918
php > $t = microtime(true);$a = range(0,25000); $b = range(15000,500000); $c =
arrayDiffEmulation($a, $b);echo microtime(true) - $t;
0.37219095230103
----
Server IP: 69.147.83.197
Probable Submitter: 89.209.68.106
----
Manual Page -- http://www.php.net/manual/en/function.array-diff.php
Edit -- https://master.php.net/note/edit/107928
Del: integrated -- https://master.php.net/note/delete/107928/integrated
Del: useless -- https://master.php.net/note/delete/107928/useless
Del: bad code -- https://master.php.net/note/delete/107928/bad+code
Del: spam -- https://master.php.net/note/delete/107928/spam
Del: non-english -- https://master.php.net/note/delete/107928/non-english
Del: in docs -- https://master.php.net/note/delete/107928/in+docs
Del: other reasons-- https://master.php.net/note/delete/107928
Reject -- https://master.php.net/note/reject/107928
Search -- https://master.php.net/manage/user-notes.php