Compare Arrays Keys and Values | PHP array_diff_assoc()

Compare Arrays Keys and Values | PHP array_diff_assoc()

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 firstThis is the first array of this function and it is required. It is an array to compare from
array secondThis is the second array of this function and it is also required. It is an array to be compared with the first array
array nIt’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:

  1. Compare Arrays PHP | PHP array_diff() Function
  2. Get, Write, Read, Load, JSON File from Url PHP
  3. Functions: Remove First Character From String PHP
  4. Remove Specific/Special Characters From String In PHP
  5. How to Replace First and Last Character From String PHP
  6. Reverse String in PHP
  7. Array Push, POP PHP | PHP Array Tutorial
  8. PHP Search Multidimensional Array By key, value and return key
  9. json_encode()- Convert Array To JSON | Object To JSON PHP
  10. PHP remove duplicates from multidimensional array
  11. 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

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *