목차
PHP获得数组交集与差集的方法,php获得数组交集
백엔드 개발 PHP 튜토리얼 PHP获得数组交集与差集的方法,php获得数组交集_PHP教程

PHP获得数组交集与差集的方法,php获得数组交集_PHP教程

Jul 13, 2016 am 09:51 AM
php 교차로 차집 정렬

PHP获得数组交集与差集的方法,php获得数组交集

本文实例讲述了PHP获得数组交集与差集的方法。分享给大家供大家参考。具体分析如下:

一、数组的交集 array_intersect()

array_intersect()函数返回一个保留了键的数组,这个数组只由第一个数组中出现的且在其他每个输入数组中都出现的值组成。其形式如下:

array array_intersect(array array1,array array2[,arrayN…])

下面这个例子将返回在$fruit1数组中出现的且在$fruit2和$fruit3中也出现的所有的水果:

<&#63;php
$fruit1 = array("Apple","Banana","Orange");
$fruit2 = array("Pear","Apple","Grape");
$fruit3 = array("Watermelon","Orange","Apple");
$intersection = array_intersect($fruit1, $fruit2, $fruit3);
print_r($intersection);
// output
// Array ( [0] => Apple )
&#63;>

로그인 후 복사

只有在两个元素相等且具有相同的数据类型时,array_intersect()函数才会认为它们是相同的。

关联数组的交集 array_intersect_assoc()

函数array_intersect_assoc()与array_intersect()基本相同,只不过他在比较中还考虑了数组的键。因此,只有在第一个数组中出现,且在所有其他输入数组中也出现的键/值对才返回到结果数组中。

形式如下:

array array_intersect_assoc(array array1,array array2[,arrayN…])

下面的例子返回了出现在$fruit1数组中,也同时出现在$fruit2与$fruit3中的所有键/值对:

<&#63;php
$fruit1 = array("red"=>"Apple","yellow"=>"Banana","orange"=>"Orange");
$fruit2 = array("yellow"=>"Pear","red"=>"Apple","purple"=>"Grape");
$fruit3 = array("green"=>"Watermelon","orange"=>"Orange","red"=>"Apple");
$intersection = array_intersect_assoc($fruit1, $fruit2, $fruit3);
print_r($intersection);
// output
// Array ( [red] => Apple )
&#63;>

로그인 후 복사

二、数组的差集 array_diff()

函数array_diff()返回出现在第一个数组中但其他输入数组中没有的值。这个功能与array_intersect()相反。

array array_diff(array array1,array array2[,arrayN…])

实例如下:

<&#63;php
$fruit1 = array("Apple","Banana","Orange");
$fruit2 = array("Pear","Apple","Grape");
$fruit3 = array("Watermelon","Orange","Apple");
$intersection = array_diff($fruit1, $fruit2, $fruit3);
print_r($intersection);
// output
// Array ( [1] => Banana )
&#63;>

로그인 후 복사

关联数组的差集 array_diff_assoc()

函数array_diff_assoc()与array_diff()基本相同,只是它在比较时还考虑了数组的键。因此,只在第一个数组中出现而不再其他输入数组中出现的键/值对才会返回到结果数组中。其形式如下:

array array_diff_assoc(array array1,array array2[,arrayN…])

下面的例子只返回了[yellow] => Banana,因为这个特殊的键/值对出现在$fruit1中,而在$fruit2和$fruit3中都不存在。

<&#63;php
$fruit1 = array("red"=>"Apple","yellow"=>"Banana","orange"=>"Orange");
$fruit2 = array("yellow"=>"Pear","red"=>"Apple","purple"=>"Grape");
$fruit3 = array("green"=>"Watermelon","orange"=>"Orange","red"=>"Apple");
$intersection = array_diff_assoc($fruit1, $fruit2, $fruit3);
print_r($intersection);
// output
// Array ( [yellow] => Banana )
&#63;>

로그인 후 복사

希望本文所述对大家的php程序设计有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1014424.htmlTechArticlePHP获得数组交集与差集的方法,php获得数组交集 本文实例讲述了PHP获得数组交集与差集的方法。分享给大家供大家参考。具体分析如下:...
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

뜨거운 기사 태그

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드 Dec 24, 2024 pm 04:42 PM

Ubuntu 및 Debian용 PHP 8.4 설치 및 업그레이드 가이드

CakePHP 날짜 및 시간 CakePHP 날짜 및 시간 Sep 10, 2024 pm 05:27 PM

CakePHP 날짜 및 시간

CakePHP 프로젝트 구성 CakePHP 프로젝트 구성 Sep 10, 2024 pm 05:25 PM

CakePHP 프로젝트 구성

CakePHP 파일 업로드 CakePHP 파일 업로드 Sep 10, 2024 pm 05:27 PM

CakePHP 파일 업로드

CakePHP 라우팅 CakePHP 라우팅 Sep 10, 2024 pm 05:25 PM

CakePHP 라우팅

CakePHP 토론 CakePHP 토론 Sep 10, 2024 pm 05:28 PM

CakePHP 토론

CakePHP 빠른 가이드 CakePHP 빠른 가이드 Sep 10, 2024 pm 05:27 PM

CakePHP 빠른 가이드

PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법 PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법 Dec 20, 2024 am 11:31 AM

PHP 개발을 위해 Visual Studio Code(VS Code)를 설정하는 방법

See all articles