In PHP, it’s often necessary to compare two or more arrays to check if they contain the same keys and values. This can be useful when you’re working with multiple arrays and need to ensure that they have the same data. 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
When working with arrays in PHP, you may come across a situation where you need to compare two or more arrays to see if they have the same keys and values. Fortunately, PHP provides a function called 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.
Note:- PHP array_diff_assoc() function compares the keys and values of two or more arrays, and it will return an array that contains the entries from array_first that are not present in array_second or array_third, and array_n, etc.
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 )
The array_diff_assoc
function returns an array that contains the keys and values from $arr1
that are not in $arr2
. In this case, the keys and values of $arr1
that are not in $arr2
are 'name' => 'John'
, 'age' => 30
, and '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 )
The array_diff_assoc
function returns an array that contains the keys and values from $arr1
that are not in $arr2
or $arr3
. In this case, the keys and values of $arr1
that are not in $arr2
or $arr3
are 'name' => 'John'
and '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. The array_diff_assoc
function is a powerful tool that can help you quickly identify differences between arrays, making it easier to work with arrays in your PHP applications.
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