note 97210 deleted from function.array-intersect by cmb
| From: | cmb@php.net | Date: | Sun, 09 Feb 2020 08:27:02 +0000 |
| Subject: | note 97210 deleted from function.array-intersect by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-214063@lists.php.net to get a copy of this message | ||
Note Submitter: james -at- bandit.co -dot- nz
----
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;
}
?>