Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • PHP Tutorial
  • PHP Exercises
  • PHP Array
  • PHP String
  • PHP Calendar
  • PHP Filesystem
  • PHP Math
  • PHP Programs
  • PHP Array Programs
  • PHP String Programs
  • PHP Interview Questions
  • PHP GMP
  • PHP IntlChar
  • PHP Image Processing
  • PHP DsSet
  • PHP DsMap
  • PHP Formatter
  • Web Technology
Open In App

PHP array_intersect_uassoc() Function

Last Updated : 20 Jun, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

The array_intersect_uassoc() function is an inbuilt function in PHP. It is used to compare key and values of two or more arrays by using a user-defined comparison function and return the matches.
The comparison function returns an integer equal to, greater than, or less than zero. If the first argument to be considered respectively less than, equal to, or greater than second. If the condition true then return a TRUE value, else it returns a FALSE value. 
Syntax: 
 

array_intersect_uassoc($array1, $array2, $array3..., uassoc_intersectFunction)

Parameters Used: This function accepts minimum three parameters and all three parameter is mandatory and the others parameters are optional. Parameters are described below: 
 

  1. $array1 (Required ): 
    The array will be compared with other arrays.. 
     
  2. $array2 (Required):
    The array Compared with the first array. 
     
  3. $array3...(Optional):
    The array Compared with the first array. 
     
  4. uassoc_intersectFunction (Required):
    It is Required user-defined function. A string that defines a callable comparison function. The comparison function returns an integer less than, equal to or greater than 0. If the first argument is less than, equal to or greater than the second argument.
     


Return Value: 
Returns an array containing the entries from array1 that are present in all other arrays such as:-(arra2, arra3, arar4....more). The return value type is an array.
 

Note: The function uses a user-defined function to compare the keys. (user-define function's 
functionality is applicable for key not for values of keys)
 

Let's take an example to understand the array_intersect_uassoc() Function. 
Program: 1 In this example we compare the keys of array by using case-insensitive strcasecmp function. It compares keys without case-sensitiveness.


 

php
<?php $arr1 = array(     "a" => "gfg",     "b" => "GeeksforGeeks",     "c" => "contribute" ); $arr2 = array(     "a" => "gfg",     "B" => "GeeksforGeeks",     "c" => "ide" ); $arr3 = array(     "x" => "gfg",     "B" => "GeeksforGeeks",     "c" => "practice" );    // Compare the keys and values by using a  // user-defined key comparison function. // Here callback function applicable on keys echo "Using function: array_intersect_uassoc() \n "; $result = array_intersect_uassoc($arr1,                  $arr2, $arr3, "strcasecmp");  // printing result print_r($result);  ?> 

Output: 
 

Using function: array_uintersect_assoc()    Array  (      [b] => GeeksforGeeks  )


 

Program: 2 Take two array (array1 and array2) and using user-defined key comparison function (uassoc_intersectFunction). Function return array with only matched keys & values only.


 

PHP
<?php  // Illustrate  array_intersect_uassoc()  // Function in PHP    function uassoc_intersectFunction($arr1, $arr2) {     if ($arr1 === $arr2) {         return 0;     }     return ($arr1 > $arr2) ? 1 : -1; }  // Code driven  $arr1 = array(     "0" => "Graph",     "1" => "Dynamic",     "3" => "Recursive",     "4" => "Prime Factor" ); $arr2 = array(     "4" => "Prime",     "2" => "Factorial",     "3" => "Recursive",     "7" => "Modulo" );  $result = array_intersect_uassoc($arr1,         $arr2, "uassoc_intersectFunction"); print_r($result);  ?> 

Output: 
 

Array  (      [3] => Recursive  )


 

Program: 3 Taking three array (arr1, arr2 and arr3) and using user-defined key comparison function (uassoc_intersectFunction). User-define function match as it is keys with same values but no any such case found then it will return NULL/Empty Array (In program 1 we used strcasecmp function this function applied on keys and ignore sensitiveness of case and return result GeeksforGeeks.)
 


 

PHP
<?php  // illustrate array_intersect_uassoc()  // Function in PHP    function uassoc_intersectFunction($arr1, $arr2) {     if ($arr1 === $arr2) {         return 0;     }     return ($arr1 > $arr2) ? 1 : -1; }  // Code driven  $arr1 = array(     "a" => "gfg",     "b" => "GeeksforGeeks",     "c" => "contribute" ); $arr2 = array(     "a" => "gfg",     "B" => "GeeksforGeeks",     "c" => "ide" ); $arr3 = array(     "a" => "Gfg",     "B" => "GeeksforGeeks",     "c" => "practice" );  // userdefine function match as it is keys // with same values but no any such // case so it will return NULL $result = array_intersect_uassoc( $arr1, $arr2, $arr3, "uassoc_intersectFunction"); print_r($result);  ?> 

Output: 
 

Array  (  )


 

Below are some related array intersect PHP functions 
 


 


  • PHP | array_intersect() Function: The function returns another array containing the elements of the first array that are present in all other arrays passed as the parameter. If no element matches then, a NULL array is returned.
  • PHP | array_intersect_assoc() Function: function returns all of the values of the first array that are present in all other arguments at the same index as that in first array, i.e., the keys are mainly used in the comparison.
  • PHP | array_uintersect_assoc() Function: compare the keys of an array and user-defined function to compare the values.
  • PHP | array_intersect_key() Function: The function returns another array containing the elements of the first array that are present in other arrays passed as the parameter whose key matches with each other. If no keys matches then, a NULL array is returned.


 

php
<?php  // Program to illustrate==> // array_intersect() function // array_intersect_assoc() function // array_uintersect_assoc() function // array_intersect_key() function  $arr1 = array(     "a" => "gfg",     "b" => "GeeksforGeeks",     "c" => "contribute" ); $arr2 = array(     "a" => "gfg",     "B" => "GeeksforGeeks",     "c" => "ide" ); $arr3 = array(     "a" => "Gfg",     "B" => "GeeksforGeeks",     "c" => "practice" );  // The array_intersect() function compares // the values (not keys) of two (or more) // arrays, and returns the matches. echo "**********array_intersect********** \n "; $result = array_intersect($arr1, $arr2, $arr3); print_r($result);   // array_intersect_assoc() returns an array  // containing all the values of arr1 that // are present in all the arguments.  // for above input it will return null echo "******array_intersect_assoc******** \n "; $result = array_intersect_assoc($arr1, $arr2, $arr3); print_r($result);  // array_uintersect_assoc compare values (data)  // by using call back function echo "*********array_uintersect_assoc********** \n "; $result = array_uintersect_assoc($arr1,                     $arr2, $arr3, "strcasecmp"); print_r($result);  // Compare the keys and values by using // a user-defined key comparison function // here callback function applicable on keys echo "*********array_uintersect_assoc *********\n "; $result = array_intersect_uassoc($arr1,                      $arr2, $arr3, "strcasecmp"); print_r($result);  // compute the intersection of two or more arrays. // function returns another array containing  // the elements of the first array that // are present in other arrays echo "*********array_intersect_key *********\n "; $result = array_intersect_key($arr1,                      $arr2, $arr3); print_r($result);   ?> 

Output: 
 

**********array_intersect**********    Array  (      [b] => GeeksforGeeks  )  ******array_intersect_assoc********    Array  (  )  *********array_uintersect_assoc**********    Array  (      [a] => gfg  )  *********array_uintersect_assoc *********   Array  (      [b] => GeeksforGeeks  )  *********array_intersect_key *********   Array  (      [a] => gfg      [c][/c] => contribute  )


 


J

jit_t
Improve
Article Tags :
  • Web Technologies
  • PHP
  • PHP-array
  • PHP-function

Similar Reads

    PHP array_chunk() Function
    The array_chunk() function is an inbuilt function in PHP which is used to split an array into parts or chunks of given size depending upon the parameters passed to the function. The last chunk may contain fewer elements than the desired size of the chunk. Syntaxarray array_chunk( $array, $size, $pre
    2 min read
    PHP array_combine() Function
    The array_combine() function is an inbuilt function in PHP which is used to combine two arrays and create a new array by using one array for keys and another array for values. That is all elements of one array will be the keys of new array and all elements of the second array will be the values of t
    2 min read
    PHP array_count_values() Function
    The array_count_values() is an inbuilt function in PHP which is used to count all the values inside an array. In other words we can say that array_count_values() function is used to calculate the frequency of all of the elements of an array. Syntax: array array_count_values( $array ) Parameters: Thi
    2 min read
    PHP array_diff_assoc() Function
    This inbuilt function of PHP is used to get the difference between one or more arrays. This function compares both the keys and values between one or more arrays and returns the difference between them. So, the function generally compares two arrays according to there keys and values and returns the
    2 min read
    PHP array_diff_key() Function
    This inbuilt function of PHP is used to get the difference between one or more arrays. This function compares the keys between one or more arrays and returns the difference between them. So, the function generally compares two arrays according to their keys and returns the elements that are present
    2 min read
    PHP array_diff_uassoc() Function
    The array_diff_uassoc() function is a built-in function in PHP and is used to get the difference between one or more arrays using an user-defined function to compare the keys. This function compares both the keys and values between one or more arrays to and returns the elements from the first array
    4 min read
    PHP array_diff_ukey() Function
    The array_diff_ukey() function is an inbuilt function in PHP. It is used to compares the key two or more arrays using a user-defined function, and return an array, which is array1 and its not present any others array2, array3 or more... Syntax: array_diff_ukey($array1, $array2, $array3..., arr_diffu
    4 min read
    PHP array_diff() function
    The array_diff() is an inbuilt function in PHP ans is used to calculate the difference between two or more arrays. This function computes difference according to the values of the elements, between one or more array and return differences in the form of a new array. This function basically returns a
    2 min read
    PHP array_fill_keys() Function
    The array_fill_keys() function is a builtin function in PHP and is used to create a new array filled with the given keys and value provided as an array to the function. Syntax: array array_fill_keys ( $keys, $value ) Parameters: This function accepts two parameters, keys and their values to be prese
    3 min read
    PHP array_fill() function
    The array_fill() is an inbuilt-function in PHP and is used to fill an array with values. This function basically creates an user-defined array with a given pre-filled value. Syntax: array_fill($start_index, $number_elements, $values) Parameter: The array_fill() function takes three parameters and ar
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences