Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP array_diff_assoc() Function.
The built-in array_diff_assoc() function in PHP compares the keys and values of two or more arrays and returns the difference.
This PHP function compares the keys and values of two or more arrays and returns an array containing the entries from array1 but are not present in array2 or array3, etc.
This function is different from array_diff() function because array_diff() function uses only values to compare with other arrays whereas array_diff_assoc() function makes use of keys and values for comparing with other arrays.
The built-in array_diff_assoc() function in PHP compares the keys and values of two or more arrays and returns the difference.
This PHP function compares the keys and values of two or more arrays and returns an array containing the entries from array1 but are not present in array2 or array3, etc.
This function is different from array_diff() function because array_diff() function uses only values to compare with other arrays whereas array_diff_assoc() function makes use of keys and values for comparing with other arrays.
Syntax
Following below is the syntax to use this function -
array array_diff_assoc( array $array1, array $array2 [, array $array3...] );
Parameter Details
Sr.No | Parameter & Description |
---|---|
1 | array1 (Required) It is an array to compare from |
2 | array2 (Required) It is an array to be compared with the first array |
3 | array3 (Optional) It is an array to be compared with the first array |
Return Value
This built-in PHP function returns an array containing the values from array1 that are not available in other arrays with the same keys.
PHP Version
This function was first lunched as part of core PHP v 4.3.0.
Example1
Try the following example below. Here "a" => "orange" and "c" => "aple" are present in both arrays, therefore it is not available in the result -
<?php $input1 = array( "a"=>"orange", "b"=>"mango", "c"=>"aple"); $input2 = array( "a"=>"orange", "b"=>"banana", "c"=>"aple"); print_r(array_diff_assoc($input1, $input2)); ?>
Output
When the above code is executed, it will produce the following result -
Array ( [b] => mango )
Example2
Here, both arrays have different keys and corresponding values for all the pairs, for example "a" => "orange" is not available in the second array, similarly other key value pairs are not present in the second array so they will be available in the result -
<?php $input1 = array( "a"=>"orange", "b"=>"mango", "c"=>"banana"); $input2 = array( "a"=>"banana", "b"=>"apple", "c"=>"orange"); print_r(array_diff_assoc($input1, $input2)); ?>
Output
When the above code is executed, it will produce the following result -
Array ( [a] => orange [b] => mango [c] => banana )
Example3
This example clarifies that two values from key => value pairs are considered equal only if the (string) $elem1 === (string) $elem2 -
<?php $input1 = array(0, 5, 20); $input2 = array("00", "05", "20"); $result = array_diff_assoc($input1, $input2); print_r($result); ?>
Output
When the above code is executed, it will produce the following result -
Array ( [0] => 0 [1] => 5 )
Alright guys! This is where we are going to be rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the PHP array_diff_key() Function.
Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.
Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.