note 109645 added to function.array-intersect-assoc
| From: | Whitend | Date: | Sat, 04 Aug 2012 08:08:55 +0000 |
| Subject: | note 109645 added to function.array-intersect-assoc | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-189918@lists.php.net to get a copy of this message | ||
One of the ways to get intersection of two arrays is as follows:
<?php
function arrayIntersect( $primary_array, $secondary_array ) {
if ( !is_array( $primary_array ) || !is_array( $secondary_array ) ) {
return false;
}
if ( !empty( $primary_array ) ) {
foreach( $primary_array as $key => $value ) {
if ( !isset( $secondary_array[$key] ) ) {
unset( $primary_array[$key] );
} else {
if ( serialize( $secondary_array[$key] ) != serialize( $value ) ) {
unset( $primary_array[$key] );
}
}
}
return $primary_array;
} else {
return array();
}
}
?>
It would pay attention to both keys and values even if values would be arrays as well. One important
note is that if value of $primary_array is yet another array, its order of key & value pairs
becomes important for matching.
----
Server IP: 69.147.83.197
Probable Submitter: 95.173.38.254
----
Manual Page -- http://www.php.net/manual/en/function.array-intersect-assoc.php
Edit -- https://master.php.net/note/edit/109645
Del: integrated -- https://master.php.net/note/delete/109645/integrated
Del: useless -- https://master.php.net/note/delete/109645/useless
Del: bad code -- https://master.php.net/note/delete/109645/bad+code
Del: spam -- https://master.php.net/note/delete/109645/spam
Del: non-english -- https://master.php.net/note/delete/109645/non-english
Del: in docs -- https://master.php.net/note/delete/109645/in+docs
Del: other reasons-- https://master.php.net/note/delete/109645
Reject -- https://master.php.net/note/reject/109645
Search -- https://master.php.net/manage/user-notes.php