array_intersect_uassoc() function in PHP


The array_intersect_unassoc() function compares array keys and values, with an additional user-made function check, and returns the matches.

Syntax

array_intersect_unassoc(arr1, arr2, arr3, arr4, …, compare_func)

Parameters

  • arr1 − Array to compare from. Required.

  • arr2 − Array to compare against. Required.

  • arr3 − You can add more arrays to compare. Optional.

  • arr4 − You can add more arrays to compare. Optional.

  • compare_func − This callback function must return an integer <, =, or > than 0 if the first argument is considered to be respectively <, =, or > than the second.

Return

The array_intersect_uassoc() function returns an array containing the entries from the first array that are not present in any of the other arrays.

Example

 Live Demo

<?php
function compare_func($a, $b) {
   if ($a === $b) {
      return 0;
   }
   return ($a > $b)? 1:-1;
}
$arr1 = array("a" => "laptop", "b" => "keyboard", "c" => "mouse");
$arr2 = array("d" => "laptop", "b" => "keyboard", "c" => "mouse");
$res = array_intersect_uassoc($arr1, $arr2, "compare_func");
print_r($res);
?>

Output

Array ( [b] => keyboard [c] => mouse )

Updated on: 24-Jun-2020

46 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements