How to use array diff assoc in PHP

In this article, I will explain how the array_diff_assoc() function can be used in PHP.
  • 2386

array_diff_assoc() function in PHP

  • The array_diff_assoc() function compares two or more arrays, and returns an array with the keys and values from the first array or retunes the difference between array1 and array2.

  • The array_diff_assoc() function, unlike array_diff() the array keys are used in the comparison.

  • In array_diff_assoc() function, both the key and the value is used in the comparison.

Syntax

array_diff_assoc(array1,array2,array3....)

Parameter

  • array1 array1 is required parameter. The first array is the array which will be compared with other arrays.

  • array2 array2 is required parameter. An array to be compared with the first array.

  • array3 array3 is optional parameter. An array to be compared with the first array.

Example

The following example show to how the array_diff_assoc() function can be used in PHP.

<h3 style="color: darkgreen;">array_diff_assoc() function example in PHP</h3>
<?php
$input_array1 = array( "a"=>"orange", "b"=>"mango", "c"=>"banana");
$input_array2 = array( "a"=>"orange", "b"=>"apple", "c"=>"banana");
print_r(array_diff_assoc($input_array1, $input_array2));
$input_array1=array(1=>"student",2=>"doctor",3=>"Advocate");
$input_array2=array(1=>"s`tudent",2=>"Engg.",3=>"Advocate");
print_r(array_diff_assoc($input_array1, $input_array2));
?>

 

Output

diff-assoc.jpg

You may also want to read these related articles here
 
Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.