Tuesday, October 20, 2020

PHP array_diff_assoc() Function



Hello folks! welcome to another section of our tutorial on PHP. In this tutorial, we will be discussing about the PHP's array_diff_assoc() Function.

PHP array_diff_assoc() function compares two or more arrays and returns the difference.

The function compares the keys and values of 2 or more arrays and returns an array which contains the entries from array1 but are not present in array2 or array3, etc.

This PHP function is different from PHP array_diff() function because array_diff() function makes use of 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.NoParameter & 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

It returns an array containing all the values from array1 that are not present in any 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. Here "a" => "orange" and "c" => "aple" are present in both arrays, so 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 too 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 makes it clear that two values from key => values 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 rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP array_diff_key() Function.

Feel free to ask your questions where necessary and i 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.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.