In this tutorial, you will learn how to PHP compare two or more array keys and values and find the difference between two or more array using the PHP array_diff_assoc() function.
How to Compare Two or More Arrays Keys and Values in PHP
The array_diff_assoc
that allows you to compare the keys and values of two or more arrays and return the differences.
PHP array_diff_assoc()
Definition: array_diff_assoc() function of in-built PHP. Basically, This can be used to get or calculate the difference between one or more arrays in PHP. PHP array_diff_assoc() function compares the keys and values between one or more arrays and returns the difference between them.
Syntax of array_diff_assoc()
The array_diff_assoc
function in PHP takes two or more arrays as arguments and returns an array that contains the difference between the keys and values of the arrays. The syntax of array_diff_assoc
is as follows:
array_diff_assoc(array1, array2, array3, ...)
Where array1
, array2
, and array3
are the arrays you want to compare.
Parameters of array_diff_assoc() function
Parameter | Description |
---|---|
array first | This is the first array of this function and it is required. It is an array to compare from |
array second | This is the second array of this function and it is also required. It is an array to be compared with the first array |
array n | It’s n array and it is optional. It is an array to be compared with the first array |
Example Usage
Suppose you have two arrays $arr1
and $arr2
, and you want to compare their keys and values. Here’s how you can use array_diff_assoc
to do so:
$arr1 = array('name' => 'John', 'age' => 30, 'country' => 'USA'); $arr2 = array('name' => 'Jane', 'age' => 25, 'country' => 'Canada'); $result = array_diff_assoc($arr1, $arr2); print_r($result);
The output of this code will be:
Array
(
[name] => John
[age] => 30
[country] => USA
)
Comparing More Than Two Arrays
You can also use array_diff_assoc
to compare more than two arrays. For example, suppose you have three arrays $arr1
, $arr2
, and $arr3
, and you want to compare their keys and values. Here’s how you can use array_diff_assoc
to do so:
$arr1 = array('name' => 'John', 'age' => 30, 'country' => 'USA'); $arr2 = array('name' => 'Jane', 'age' => 25, 'country' => 'Canada'); $arr3 = array('name' => 'Mary', 'age' => 28, 'country' => 'USA'); $result = array_diff_assoc($arr1, $arr2, $arr3); print_r($result);
The output of the above code is:
Array
(
[name] => John
[age] => 30
)
Conclusion
In this article, you have learned how to use the array_diff_assoc
function in PHP to compare two or more arrays and return the differences in their keys and values.
Recommended Posts:
- Compare Arrays PHP | PHP array_diff() Function
- Get, Write, Read, Load, JSON File from Url PHP
- Functions: Remove First Character From String PHP
- Remove Specific/Special Characters From String In PHP
- How to Replace First and Last Character From String PHP
- Reverse String in PHP
- Array Push, POP PHP | PHP Array Tutorial
- PHP Search Multidimensional Array By key, value and return key
- json_encode()- Convert Array To JSON | Object To JSON PHP
- PHP remove duplicates from multidimensional array
- PHP Remove Duplicate Elements or Values from Array PHP
If you want to know more about this function, you can visit the office PHP SITE:
http://php.net/manual/en/function.array-diff-assoc.php