search
HomeBackend DevelopmentPHP ProblemWhat 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.

What are the functions for finding the intersection of two arrays in PHP?

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(&#39;content-type:text/html;charset=utf-8&#39;);   
$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);
?>

What are the functions for finding the intersection of two arrays in PHP?

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(&#39;content-type:text/html;charset=utf-8&#39;);   
$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);
?>

What are the functions for finding the intersection of two arrays in PHP?

3. array_intersect_key() function

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$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);
?>

What are the functions for finding the intersection of two arrays in PHP?

Description: No Commonly used comparison functions

  • array_intersect_uassoc()

  • array_intersect_ukey()

  • array_uintersect()

  • ##array_uintersect_assoc()

  • array_uintersect_uassoc()


They all use user-defined functions to compare functions

Example:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
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);
?>

What are the functions for finding the intersection of two arrays in PHP?

Recommended learning: "

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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

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

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft