PHP 8.3.21 Released!

Voting

: nine plus zero?
(Example: nine)

The Note You're Voting On

Whitend
12 years ago
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.

<< Back to user notes page

To Top