note 97210 added to function.array-intersect
| From: | james-at-bandit dot co-dot-nz at osu1 dot php dot net | Date: | Thu, 08 Apr 2010 13:07:49 +0000 |
| Subject: | note 97210 added to function.array-intersect | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-168012@lists.php.net to get a copy of this message | ||
I needed an array_intersect that would delete the intersecting values from the original array.
Voila:
<?php
// array_intersect that splits the needle array into two - one filled with "intersected"
results, and one filled with the remainder
function array_intersect_split(&$needle, $haystack, $preserve_keys = false) {
if(!is_array($needle) || !is_array($haystack)) return false;
$new_arr = array();
foreach($needle as $key => $value) {
if(($loc = array_search($value, $haystack))!==false) {
if(!$preserve_keys) $new_arr[] = $value;
else $new_arr[$key] = $value;
unset($needle[$key]);
}
}
return $new_arr;
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 118.90.1.31
----
Manual Page -- http://www.php.net/manual/en/function.array-intersect.php
Edit -- https://master.php.net/note/edit/97210
Del: integrated -- https://master.php.net/note/delete/97210/integrated
Del: useless -- https://master.php.net/note/delete/97210/useless
Del: bad code -- https://master.php.net/note/delete/97210/bad+code
Del: spam -- https://master.php.net/note/delete/97210/spam
Del: non-english -- https://master.php.net/note/delete/97210/non-english
Del: in docs -- https://master.php.net/note/delete/97210/in+docs
Del: other reasons-- https://master.php.net/note/delete/97210
Reject -- https://master.php.net/note/reject/97210
Search -- https://master.php.net/manage/user-notes.php