


What are the functions for finding the intersection of two arrays in PHP?
There are 8 intersection functions: 1. array_intersect(), which only compares key values; 2. array_intersect_assoc(), which compares key names and key values; 3. array_intersect_key(), which only compares key names; 4. array_uintersect( )wait.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php provides multiple requests for example Array intersection function:
array_intersect(): compares arrays and returns the intersection of two arrays (only comparing key values).
array_intersect_assoc(): Compare arrays and return the intersection of two arrays (compare key names and key values).
array_intersect_key(): Compares arrays and returns the intersection of two arrays (only comparing key names).
array_intersect_uassoc(): Compare arrays and return the intersection of two arrays (compare key names and key values, using user-defined comparison functions).
array_intersect_ukey(): Compares arrays and returns the intersection of two arrays (only compares key names, using user-defined comparison functions).
array_uintersect(): Compares arrays and returns the intersection of two arrays (only compares key values, using a user-defined comparison function).
array_uintersect_assoc(): Compare arrays and return the intersection of two arrays (compare key names and key values, use built-in functions to compare, use user-defined functions to compare key values).
array_uintersect_uassoc(): Compare arrays and return the intersection of two arrays (compare key names and key values, using two user-defined comparison functions).
The following is an introduction to the comparison functions commonly used to find the intersection of arrays
1. array_intersect() function
array_intersect() function Used to compare the values of two (or more) arrays and return the intersection.
This function compares the values of two (or more) arrays and returns an intersection array containing all values in array1 that also appear in all other parameter arrays.
<?php header('content-type:text/html;charset=utf-8'); $a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"); $a2=array("e"=>"red","f"=>"green","g"=>"blue"); $result=array_intersect($a1,$a2); var_dump($result); ?>
2. array_intersect_assoc() function
array_intersect_assoc() function is used to compare two (or more) arrays The key name and key value, and return the intersection.
This function compares the key names and key values of two (or more) arrays, and returns an intersection array, which includes everything in the compared array (array1) and any other The key name and key value in the parameter array (array2 or array3, etc.).
<?php header('content-type:text/html;charset=utf-8'); $a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow"); $a2=array("a"=>"red","b"=>"green","c"=>"blue"); $result=array_intersect_assoc($a1,$a2); var_dump($result); ?>
3. array_intersect_key() function
<?php header('content-type:text/html;charset=utf-8'); $a1=array("a"=>"red","b"=>"green","c"=>"blue"); $a2=array("a"=>"red","c"=>"blue","d"=>"pink"); $result=array_intersect_key($a1,$a2); var_dump($result); ?>
Description: No Commonly used comparison functions
array_intersect_uassoc()
array_intersect_ukey()
array_uintersect()
- ##array_uintersect_assoc()
- array_uintersect_uassoc()
<?php header('content-type:text/html;charset=utf-8'); function myfunction($a,$b) { if ($a===$b) { return 0; } return ($a>$b)?1:-1; } $a1=array("a"=>"red","b"=>"green","c"=>"blue"); $a2=array("d"=>"red","b"=>"green","e"=>"blue"); $result=array_intersect_uassoc($a1,$a2,"myfunction"); var_dump($result); ?>
PHP Video Tutorial"
The above is the detailed content of What are the functions for finding the intersection of two arrays in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
