PHP array_diff_assoc() function

PHP array_diff() function
PHP array_diff_key function

In PHP, the array_diff_assoc() function is used to compare two or more arrays and return the differences between them based on both the keys and the values. The function compares the keys and values of each array element and returns an array containing the elements from the first array that are not present in any of the other arrays.

The array_diff_assoc() function takes one or more arrays as its parameters and returns an array containing the differences. For example:

$array1 = array("a" => "red", "b" => "green", "c" => "blue");
$array2 = array("a" => "red", "b" => "yellow", "d" => "purple");
$result = array_diff_assoc($array1, $array2);
print_r($result);

The above code will output:

Array
(
    [b] => green
    [c] => blue
)

As you can see, the array_diff_assoc() function compares the keys and values of each array element and returns an array containing the elements from the first array that are not present in the second array. In this example, the key “b” has a different value in the two arrays, so it is included in the result.

You can also compare more than two arrays by passing additional arrays as parameters to the array_diff_assoc() function. For example:

$array1 = array("a" => "red", "b" => "green", "c" => "blue");
$array2 = array("a" => "red", "b" => "yellow", "d" => "purple");
$array3 = array("a" => "red", "b" => "green", "e" => "pink");
$result = array_diff_assoc($array1, $array2, $array3);
print_r($result);

The above code will output:

Array
(
    [c] => blue
)
PHP array_diff() function
PHP array_diff_key function

Stay up-to-date about PHP!

We don’t spam!

en English
X
Scroll to Top