Home > Backend Development > PHP Problem > What are the functions for finding the intersection of two arrays in PHP?

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

青灯夜游
Release: 2023-03-15 22:32:01
Original
3322 people have browsed it

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);
?>
Copy after login

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);
?>
Copy after login

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);
?>
Copy after login

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);
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template