Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial php 数组求交集问题

php 数组求交集问题

Jun 23, 2016 pm 02:01 PM

php 数组函数 交集

看手册看到这几个方法,不是很明白。哪位大神帮忙解释一下

array_intersect(array1, array2) //比较值 取交集
array_intersect_assoc()         // 比较键名和值 取交集
array_intersect_key(array1, array2)  // 比较键名 取交集

下边这两个不是很明白
array_intersect_uassoc(array1,array2,array3...,function) 
array_intersect_ukey(array1,array2,array3...,function)

function 的返回值有什么作用吗?

最好能举个实际中用到的例子解释一下。

回复讨论(解决方案)

function参数表示一个方法就是 array1,array2,array3等数组里面的值或者键值都会去执行这个函数

function  key_compare_func ( $key1 ,  $key2 ){    if ( $key1  ==  $key2 )        return  0 ;    else if ( $key1  >  $key2 )        return  1 ;    else        return - 1 ;} $array1  = array( 'blue'   =>  1 ,  'red'   =>  2 ,  'green'   =>  3 ,  'purple'  =>  4 ); $array2  = array( 'green'  =>  5 ,  'blue'  =>  6 ,  'yellow'  =>  7 ,  'cyan'    =>  8 ); var_dump ( array_intersect_ukey ( $array1 ,  $array2 ,  'key_compare_func' ));
Copy after login

输出结果:
array(2) {
["blue"]=>
int(1)
["green"]=>
int(3)
}
这些例子php手册下面都有实例的

求两个数组的交集问题可以使用array_intersect(),array_inersect_assoc,array_intersect_key来实现,其中array_intersect()函数是求两个数的交集

<?PHP $array = array("red"=>"Red","green"=>"red4","Red15"=>"Red",7=>"Level","Width"=>"Red","azzzz1"=>"art","peak"=>158); $array1 = array("red"=>"Red2","greena"=>"red","Red15"=>"Red",7=>"Level","Width"=>"Red","azzzz"=>"art","peak"=>158); $num = array_intersect($array,$array1); print_r ($num); echo ""; $num = array_intersect_assoc($array,$array1); print_r($num); echo ""; $num = array_intersect_key($array,$array1); print_r ($num); ?>
Copy after login


详细请看 http://web10000.cn/thread-126-1-1.html
希望能帮助到你

以 #1 的代码为例

function  key_compare_func ( $key1 ,  $key2 ) {    if ( $key1  ==  $key2 )        return  0 ;    else if ( $key1  >  $key2 )        return  1 ;    else        return - 1 ;} $array1  = array( 'blue'   =>  1 ,  'red'   =>  2 ,  'green'   =>  3 ,  'purple'  =>  4 );$array2  = array( 'green'  =>  5 ,  'blue'  =>  6 ,  'yellow'  =>  7 ,  'cyan'    =>  8 );print_r( array_intersect_key ( $array1 ,  $array2 ));print_r( array_intersect_ukey ( $array1 ,  $array2 ,  'key_compare_func' ));
Copy after login
Copy after login
Array(    [blue] => 1    [green] => 3)Array(    [blue] => 1    [green] => 3)
Copy after login
Copy after login

可知 array_intersect_key 内部使用了与 key_compare_func 等价的函数
array_intersect_ukey 可以通过回调函数改变默认行为
function  key_compare_func ( $key1 ,  $key2 ) {    if ( strtolower($key1)  ==  strtolower($key2) )        return  0 ;    else if ( $key1 > $key2 )        return  1 ;    else        return - 1 ;} $array1  = array( 'Blue'   =>  1 ,  'red'   =>  2 ,  'green'   =>  3 ,  'purple'  =>  4 );$array2  = array( 'green'  =>  5 ,  'blue'  =>  6 ,  'yellow'  =>  7 ,  'cyan'    =>  8 );print_r( array_intersect_key ( $array1 ,  $array2 ));print_r( array_intersect_ukey ( $array1 ,  $array2 ,  'key_compare_func' ));
Copy after login
Copy after login
Array(    [green] => 3)Array(    [Blue] => 1    [green] => 3
Copy after login
Copy after login
可以看到,比较时已不再区分大小写了

以 #1 的代码为例

function  key_compare_func ( $key1 ,  $key2 ) {    if ( $key1  ==  $key2 )        return  0 ;    else if ( $key1  >  $key2 )        return  1 ;    else        return - 1 ;} $array1  = array( 'blue'   =>  1 ,  'red'   =>  2 ,  'green'   =>  3 ,  'purple'  =>  4 );$array2  = array( 'green'  =>  5 ,  'blue'  =>  6 ,  'yellow'  =>  7 ,  'cyan'    =>  8 );print_r( array_intersect_key ( $array1 ,  $array2 ));print_r( array_intersect_ukey ( $array1 ,  $array2 ,  'key_compare_func' ));
Copy after login
Copy after login
Array(    [blue] => 1    [green] => 3)Array(    [blue] => 1    [green] => 3)
Copy after login
Copy after login

可知 array_intersect_key 内部使用了与 key_compare_func 等价的函数
array_intersect_ukey 可以通过回调函数改变默认行为
function  key_compare_func ( $key1 ,  $key2 ) {    if ( strtolower($key1)  ==  strtolower($key2) )        return  0 ;    else if ( $key1 > $key2 )        return  1 ;    else        return - 1 ;} $array1  = array( 'Blue'   =>  1 ,  'red'   =>  2 ,  'green'   =>  3 ,  'purple'  =>  4 );$array2  = array( 'green'  =>  5 ,  'blue'  =>  6 ,  'yellow'  =>  7 ,  'cyan'    =>  8 );print_r( array_intersect_key ( $array1 ,  $array2 ));print_r( array_intersect_ukey ( $array1 ,  $array2 ,  'key_compare_func' ));
Copy after login
Copy after login
Array(    [green] => 3)Array(    [Blue] => 1    [green] => 3
Copy after login
Copy after login
可以看到,比较时已不再区分大小写了

array_intersect_ukey明白了。
那array_intersect_uassoc函数,手册上说:“注意,与 array_intersect_assoc() 不同的是除了比较键值,还要比较键名” 手册上给的例子,function中只是比较了值。并没有提到键的比较呢。

function  key_compare_func ( $key1 ,  $key2 ) {    if ( strtolower($key1)  ==  strtolower($key2) )        return  0 ;    else if ( $key1 > $key2 )        return  1 ;    else        return - 1 ;} $array1  = array( 'Blue'   =>  1 ,  'red'   =>  2 ,  'green'   =>  3 ,  'purple'  =>  4 );$array2  = array( 'blue'  =>  1 ,  'green'  =>  5 ,  'yellow'  =>  7 ,  'cyan'    =>  8 );print_r( array_intersect_assoc ( $array1 ,  $array2 ));print_r( array_intersect_uassoc ( $array1 ,  $array2 ,  'key_compare_func' ));
Copy after login
Copy after login
Array()Array(    [Blue] => 1)
Copy after login
Copy after login
看出来了吗?

function  key_compare_func ( $key1 ,  $key2 ) {    if ( strtolower($key1)  ==  strtolower($key2) )        return  0 ;    else if ( $key1 > $key2 )        return  1 ;    else        return - 1 ;} $array1  = array( 'Blue'   =>  1 ,  'red'   =>  2 ,  'green'   =>  3 ,  'purple'  =>  4 );$array2  = array( 'blue'  =>  1 ,  'green'  =>  5 ,  'yellow'  =>  7 ,  'cyan'    =>  8 );print_r( array_intersect_assoc ( $array1 ,  $array2 ));print_r( array_intersect_uassoc ( $array1 ,  $array2 ,  'key_compare_func' ));
Copy after login
Copy after login
Array()Array(    [Blue] => 1)
Copy after login
Copy after login
看出来了吗?
怎样区分,function的参数是‘键’还是‘值’?

不需区分
array_intersect_uassoc 就是用回调函数判别键

如果要自己判断值和键,则要用 array_uintersect_uassoc 函数
并传递两个回调函数

不需区分
array_intersect_uassoc 就是用回调函数判别键

如果要自己判断值和键,则要用 array_uintersect_uassoc 函数
并传递两个回调函数

奥 明白了。 谢谢! 

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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

See all articles