Current Location: Home> Latest Articles> How to implement the array_diff_assoc() function function function of multidimensional arrays using recursive methods?

How to implement the array_diff_assoc() function function function of multidimensional arrays using recursive methods?

M66 2025-06-06

Suppose we have two multidimensional arrays, we want to compare these two arrays and return those elements that appear in the first array but do not exist in the second array. We can recursively iterate through each layer of the array and perform array_diff_assoc() for comparison.

Here is the sample code for implementing array_diff_assoc() recursively using PHP:

 <?php

/**
 * Recursively implement multi-dimensional arrays array_diff_assoc() Function
 *
 * @param array $array1 The first array
 * @param array $array2 The second array
 * @return array Returns the result of the difference between two arrays
 */
function recursive_array_diff_assoc($array1, $array2) {
    $difference = array();

    // 遍历The first array
    foreach ($array1 as $key => $value) {
        // 如果The second array没有此键,Or the value of this key is different
        if (!array_key_exists($key, $array2)) {
            // If the value is an array,Recursive comparison
            $difference[$key] = is_array($value) ? recursive_array_diff_assoc($value, array()) : $value;
        } else {
            // If the value is an array,Recursive comparison
            if (is_array($value) && is_array($array2[$key])) {
                $recursive_diff = recursive_array_diff_assoc($value, $array2[$key]);
                if (!empty($recursive_diff)) {
                    $difference[$key] = $recursive_diff;
                }
            } elseif ($value !== $array2[$key]) {
                // If the value is different,Add to Difference
                $difference[$key] = $value;
            }
        }
    }

    return $difference;
}

// Example array
$array1 = array(
    'a' => 1,
    'b' => array(
        'x' => 2,
        'y' => 3
    ),
    'c' => 4
);

$array2 = array(
    'a' => 1,
    'b' => array(
        'x' => 2,
        'y' => 4
    ),
    'c' => 5
);

// Calling recursive methods
$result = recursive_array_diff_assoc($array1, $array2);

// Print results
print_r($result);

?>

Code parsing

  1. Recursive_array_diff_assoc() function :

    • This function takes two arrays as parameters: $array1 and $array2 .

    • We traverse each key-value pair in the first array by foreach .

    • For each element, we first check whether the key is included in the second array, and if not, add the element directly to the return result.

    • If the element is an array, we continue to compare multidimensional arrays using recursive_array_diff_assoc() .

    • Finally, all the different elements are returned.

  2. Recursive processing :

    • When encountering an array element, we call the same function recursively to ensure that each layer in a multi-dimensional array is compared.

    • If the element encountered is a basic data type (such as integers, strings, etc.), we directly compare its values.

  3. Results output :

    • print_r($result) is used to output the result. In the example, after $array1 and $array2 are compared, the output contains the difference between the two.

Sample output

 Array
(
    [b] => Array
        (
            [y] => 3
        )
    [c] => 4
)

Summarize

Through recursive mode, we can implement the array_diff_assoc() function of multi-dimensional arrays in PHP. This method ensures that differences in key names and values ​​at each level can be handled correctly even when there are nested subarrays in the array.