We now have a youtube channel. Subscribe!

PHP array_diff_uassoc() Function

PHP array_diff_uassoc() Function


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_uassoc() Function.

The PHP array_diff_uassoc() function compares the keys and values of two (or more) arrays, and then it returns an array that contains the entries from array1 that are not present in any other arrays with same value.

This PHP function is different from the array_diff() function because the array_diff() function compares the values whereas this function compares the keys and then their values in other arrays.

This PHP function is different from array_diff_assoc() because PHP array_diff_assoc() function makes use of the internal algorithm to compare the keys and their values where as this PHP function makes use of the user defined functions to compare the keys and values.

Syntax

Following below is the syntax to use this function -

array_diff_uassoc ( $array1, $array2 [, $array3..., callback $key_compare_func] );


Parameter Details

Sr.NoParameter & Description
1

array1(Required)

The array to compare from

2

array2(Required)

This is an array to be compared with the first array

3

array3(Optional)

An array to be compared with the first array

4

key_compare_func(Required)

It is a callback function to use to compare the keys. This callback function must return an integer <, =, > than 0 if the first argument is considered to be respectively <, =, > than the second argument.


Return Value

This built-in PHP function returns an array containing all the entries from array1 that are not present in any other arrays.

PHP Version

This function was first lunched as part of core PHP v 5.0.0.

Example1

Try out the below example. Here key comparison function returns 0 if $input1 keys are equal to any other input array, else it returns 1 if greater and -1 if smaller.

While comparing keys using the defined function, we do have the keys "a" in both arrays, so it will not display in the output. Next keys "b" and "c" are not in the second array so it is going to be displayed in output. Additionally pair 0 => "red" is in the output because in the second argument "red" has a key which is 1 -

<?php
   function key_compare_func($a, $b) {
      if ($a === $b) {
         return 0;
      }
      return ($a > $b)? 1: -1;
   }

   $input1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
   $input2 = array("a" => "green", "yellow", "red");

   $result = array_diff_uassoc($input1, $input2, "key_compare_func");

   print_r($result);

?>

Output

When the above code is executed, it will produce the following result -

Array
(
    [b] => brown
    [c] => blue
    [0] => red
)

Example2

Try the following example. This time "red" will not be in the output because both keys are equal to each other which is 0 -

<?php
   function key_compare_func($a, $b) {
      if ($a === $b) {
         return 0;
      }
      return ($a > $b)? 1: -1;
   }

   $input1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
   $input2 = array("a" => "green", "c" => "yellow", "red");

   $result = array_diff_uassoc($input1, $input2, "key_compare_func");

   print_r($result);

?>

Output

When the above code is executed, it will produce the following result -

Array
(
    [b] => brown
    [c] => blue
)


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_ukey() 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.

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.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain