Current Location: Home> Latest Articles> Practical Methods for Comparing and Filtering Complex Multidimensional Arrays with the array_intersect_uassoc Function

Practical Methods for Comparing and Filtering Complex Multidimensional Arrays with the array_intersect_uassoc Function

gitbox 2025-06-16

In PHP, the array_intersect_uassoc function is a powerful tool that allows developers to compare and filter two or more arrays, especially effective when working with complex multidimensional arrays. This function lets us compare the keys and values of arrays based on a user-defined callback function, retrieving items that meet certain conditions. In this article, we will explore how to use array_intersect_uassoc to handle complex multidimensional arrays, with practical examples.

Introduction to array_intersect_uassoc Function

The array_intersect_uassoc function returns the intersection of two or more arrays, using a custom comparison function for both keys and values. The function definition is as follows:

array_intersect_uassoc(array $array1, array $array2, callable $value_func)

Parameter Explanation:

  • $array1: The first array.

  • $array2: The second array.

  • $value_func: A custom callback function for comparing the keys and values of the arrays.

Unlike the regular array_intersect function, which compares only the values of the arrays, array_intersect_uassoc allows you to specify a custom function to compare both keys and values.

Use Cases

We often encounter situations where more precise filtering is needed when dealing with multidimensional arrays, especially when we care more about matching keys or complex conditions rather than just array values. array_intersect_uassoc is an ideal tool for these cases.

For example, in e-commerce data processing, we may need to filter product information from two different arrays based on multiple criteria like price, quantity, SKU, etc. In such cases, array_intersect_uassoc becomes invaluable.

Practical Case: Comparing Two Product Lists

Suppose we have two multidimensional arrays containing detailed information about different products. We need to filter out products that match certain criteria, such as the product's sku and price, from these two arrays.

Sample Data

$array1 = [
    101 => ['sku' => 'A123', 'price' => 100, 'name' => 'Product 1'],
    102 => ['sku' => 'B234', 'price' => 150, 'name' => 'Product 2'],
    103 => ['sku' => 'C345', 'price' => 200, 'name' => 'Product 3'],
];
<p>$array2 = [<br>
201 => ['sku' => 'A123', 'price' => 100, 'name' => 'Product 1'],<br>
202 => ['sku' => 'B234', 'price' => 140, 'name' => 'Product 2'],<br>
203 => ['sku' => 'D456', 'price' => 250, 'name' => 'Product 4'],<br>
];<br>

Using array_intersect_uassoc to Filter Matching Products

We need a callback function to compare whether the sku and price are equal:

function compare_product($a, $b) {
    // Compare sku and price
    return ($a[&#039;sku&#039;] === $b[&#039;sku&#039;] && $a[&#039;price&#039;] === $b[&#039;price&#039;]) ? 0 : 1;
}
<p>$result = array_intersect_uassoc($array1, $array2, 'compare_product');</p>
<p>print_r($result);<br>

Output:

Array
(
    [101] => Array
        (
            [sku] => A123
            [price] => 100
            [name] => Product 1
        )
)

In this example, we have defined the compare_product function, and only when the sku and price are the same in both arrays, array_intersect_uassoc will keep these matching products.

Extended Use Cases

1. Filtering Based on Complex Conditions

When we need to compare and filter arrays based on multiple attributes, array_intersect_uassoc is a very useful tool. For example, if we want to compare the sku, price, and name of products, we can simply modify the callback function.

function compare_complex_product($a, $b) {
    return ($a[&#039;sku&#039;] === $b[&#039;sku&#039;] && $a[&#039;price&#039;] === $b[&#039;price&#039;] && $a[&#039;name&#039;] === $b[&#039;name&#039;]) ? 0 : 1;
}

2. Filtering Intersections from Multiple Arrays

array_intersect_uassoc is not limited to comparing two arrays; it can also filter intersections from multiple arrays. For instance, when you need to compare more than three arrays, you can still use the function by passing additional arrays as parameters.

$array3 = [
    301 => [&#039;sku&#039; => &#039;A123&#039;, &#039;price&#039; => 100, &#039;name&#039; => &#039;Product 1&#039;],
    302 => [&#039;sku&#039; => &#039;C345&#039;, &#039;price&#039; => 200, &#039;name&#039; => &#039;Product 3&#039;],
];
<p>$result = array_intersect_uassoc($array1, $array2, $array3, 'compare_product');<br>

Conclusion

array_intersect_uassoc is a powerful tool for working with complex multidimensional arrays. By using a custom callback function, you can compare arrays based on keys and values, easily filtering out items that meet specific conditions. Whether handling simple key-value comparisons or filtering complex multidimensional arrays, it provides great flexibility and utility.

If you haven’t used this function yet, give it a try in your projects to solve complex data processing problems.